Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | /* * linux/kernel/chr_drv/mem.c * * (C) 1991 Linus Torvalds */ #include <errno.h> #include <sys/types.h> #include <linux/sched.h> #include <linux/kernel.h> #include <linux/tty.h> #include <asm/segment.h> #include <asm/io.h> static int read_ram(struct inode * inode, struct file * file,char * buf, int count) { return -EIO; } static int write_ram(struct inode * inode, struct file * file,char * buf, int count) { return -EIO; } static int read_mem(struct inode * inode, struct file * file,char * buf, int count) { unsigned long addr; char *tmp; unsigned long pde, pte, page; int i; if (count < 0) return -EINVAL; addr = file->f_pos; tmp = buf; while (count > 0) { pde = (unsigned long) pg_dir + (addr >> 20 & 0xffc); if (!((pte = *((unsigned long *) pde)) & 1)) break; pte &= 0xfffff000; pte += (addr >> 10) & 0xffc; if (((page = *((unsigned long *) pte)) & 1) == 0) break; /* if ((page & 2) == 0) un_wp_page((unsigned long *) pte); */ page &= 0xfffff000; page += addr & 0xfff; i = 4096-(addr & 0xfff); if (i > count) i = count; memcpy_tofs(tmp,(void *) page,i); addr += i; tmp += i; count -= i; } file->f_pos = addr; return tmp-buf; } static int write_mem(struct inode * inode, struct file * file,char * buf, int count) { unsigned long addr; char *tmp; unsigned long pde, pte, page; int i; if (count < 0) return -EINVAL; addr = file->f_pos; tmp = buf; while (count > 0) { pde = (unsigned long) pg_dir + (addr >> 20 & 0xffc); if (!((pte = *((unsigned long *) pde)) & 1)) break; pte &= 0xfffff000; pte += (addr >> 10) & 0xffc; if (((page = *((unsigned long *) pte)) & 1) == 0) break; if ((page & 2) == 0) un_wp_page((unsigned long *) pte); page &= 0xfffff000; page += addr & 0xfff; i = 4096-(addr & 0xfff); if (i > count) i = count; memcpy_fromfs((void *) page,tmp,i); addr += i; tmp += i; count -= i; } file->f_pos = addr; return tmp-buf; } static int read_kmem(struct inode * inode, struct file * file,char * buf, int count) { unsigned long p = file->f_pos; if (count < 0) return -EINVAL; if (p >= HIGH_MEMORY) return 0; if (count > HIGH_MEMORY - p) count = HIGH_MEMORY - p; memcpy_tofs(buf,(void *) p,count); file->f_pos += count; return count; } static int write_kmem(struct inode * inode, struct file * file,char * buf, int count) { unsigned long p = file->f_pos; if (count < 0) return -EINVAL; if (p >= HIGH_MEMORY) return 0; if (count > HIGH_MEMORY - p) count = HIGH_MEMORY - p; memcpy_fromfs((void *) p,buf,count); file->f_pos += count; return count; } static int read_port(struct inode * inode,struct file * file,char * buf, int count) { unsigned int i = file->f_pos; char * tmp = buf; while (count-- > 0 && i < 65536) { put_fs_byte(inb(i),tmp); i++; tmp++; } file->f_pos = i; return tmp-buf; } static int write_port(struct inode * inode,struct file * file,char * buf, int count) { unsigned int i = file->f_pos; char * tmp = buf; while (count-- > 0 && i < 65536) { outb(get_fs_byte(tmp),i); i++; tmp++; } file->f_pos = i; return tmp-buf; } static int read_zero(struct inode *node,struct file *file,char *buf,int count) { int left; for (left = count; left > 0; left--) { put_fs_byte(0,buf); buf++; } return count; } /* * The memory devices use the full 32 bits of the offset, and so we cannot * check against negative addresses: they are ok. The return value is weird, * though, in that case (0). * * also note that seeking relative to the "end of file" isn't supported: * it has no meaning, so it returns -EINVAL. */ static int mem_lseek(struct inode * inode, struct file * file, off_t offset, int orig) { switch (orig) { case 0: file->f_pos = offset; return file->f_pos; case 1: file->f_pos += offset; return file->f_pos; default: return -EINVAL; } if (file->f_pos < 0) return 0; return file->f_pos; } static int mem_read(struct inode * inode, struct file * file, char * buf, int count) { switch (MINOR(inode->i_rdev)) { case 0: return read_ram(inode,file,buf,count); case 1: return read_mem(inode,file,buf,count); case 2: return read_kmem(inode,file,buf,count); case 3: return 0; /* /dev/null */ case 4: return read_port(inode,file,buf,count); case 5: return read_zero(inode,file,buf,count); default: return -ENODEV; } } static int mem_write(struct inode * inode, struct file * file, char * buf, int count) { switch (MINOR(inode->i_rdev)) { case 0: return write_ram(inode,file,buf,count); case 1: return write_mem(inode,file,buf,count); case 2: return write_kmem(inode,file,buf,count); case 3: return count; /* /dev/null */ case 4: return write_port(inode,file,buf,count); case 5: return count; /* /dev/zero */ default: return -ENODEV; } } static struct file_operations mem_fops = { mem_lseek, mem_read, mem_write, NULL, /* mem_readdir */ NULL, /* mem_select */ NULL, /* mem_ioctl */ NULL, /* no special open code */ NULL /* no special release code */ }; long chr_dev_init(long kmem_start) { chrdev_fops[1] = &mem_fops; kmem_start = tty_init(kmem_start); kmem_start = lp_init(kmem_start); return kmem_start; } |