|
|
1.1 root 1: /*
2: * linux/mm/mmap.c
3: *
4: * Written by obz.
5: */
1.1.1.2 root 6: #include <linux/stat.h>
1.1 root 7: #include <linux/sched.h>
8: #include <linux/kernel.h>
1.1.1.3 ! root 9: #include <linux/mm.h>
1.1 root 10: #include <asm/segment.h>
11: #include <asm/system.h>
12: #include <errno.h>
13: #include <sys/mman.h>
14:
15: /*
16: * description of effects of mapping type and prot in current implementation.
17: * this is due to the current handling of page faults in memory.c. the expected
18: * behavior is in parens:
19: *
20: * map_type prot
21: * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
22: * MAP_SHARED r: (no) yes r: (yes) yes r: (no) yes r: (no) no
23: * w: (no) yes w: (no) copy w: (yes) yes w: (no) no
24: * x: (no) no x: (no) no x: (no) no x: (yes) no
25: *
26: * MAP_PRIVATE r: (no) yes r: (yes) yes r: (no) yes r: (no) no
27: * w: (no) copy w: (no) copy w: (copy) copy w: (no) no
28: * x: (no) no x: (no) no x: (no) no x: (yes) no
29: *
30: * the permissions are encoded as cxwr (copy,exec,write,read)
31: */
32: #define MTYP(T) ((T) & MAP_TYPE)
33: #define PREAD(T,P) (((P) & PROT_READ) ? 1 : 0)
34: #define PWRITE(T,P) (((P) & PROT_WRITE) ? (MTYP(T) == MAP_SHARED ? 2 : 10) : 0)
35: #define PEXEC(T,P) (((P) & PROT_EXEC) ? 4 : 0)
36: #define PERMISS(T,P) (PREAD(T,P)|PWRITE(T,P)|PEXEC(T,P))
37:
38: #define CODE_SPACE(addr) ((((addr)+4095)&~4095) < \
39: current->start_code + current->end_code)
40:
41: static caddr_t
42: mmap_chr(unsigned long addr, size_t len, int prot, int flags,
43: struct inode *inode, unsigned long off)
44: {
45: int major, minor;
46:
47: major = MAJOR(inode->i_rdev);
48: minor = MINOR(inode->i_rdev);
49:
50: /*
51: * for character devices, only /dev/mem may be mapped. when the
52: * swapping code is modified to allow arbitrary sources of pages,
53: * then we can open it up to regular files.
54: */
55:
56: if (major != 1 || minor != 1)
57: return (caddr_t)-ENODEV;
58:
59: /*
60: * we only allow mappings from address 0 to HIGH_MEMORY, since thats
61: * the range of our memory [actually this is a lie. the buffer cache
62: * and ramdisk occupy higher memory, but the paging stuff won't
63: * let us map to it anyway, so we break it here].
64: *
65: * this call is very dangerous! because of the lack of adequate
66: * tagging of frames, it is possible to mmap over a frame belonging
67: * to another (innocent) process. with MAP_SHARED|MAP_WRITE, this
68: * rogue process can trample over the other's data! we ignore this :{
69: * for now, we hope people will malloc the required amount of space,
70: * then mmap over it. the mm needs serious work before this can be
71: * truly useful.
72: */
73:
74: if (len > HIGH_MEMORY || off > HIGH_MEMORY - len) /* avoid overflow */
75: return (caddr_t)-ENXIO;
76:
77: if (remap_page_range(addr, off, len, PERMISS(flags, prot)))
78: return (caddr_t)-EAGAIN;
79:
80: return (caddr_t)addr;
81: }
82:
83: caddr_t
84: sys_mmap(unsigned long *buffer)
85: {
86: unsigned long base, addr;
87: unsigned long len, limit, off;
88: int prot, flags, fd;
89: struct file *file;
90: struct inode *inode;
91:
92: addr = (unsigned long) get_fs_long(buffer); /* user address space*/
93: len = (size_t) get_fs_long(buffer+1); /* nbytes of mapping */
94: prot = (int) get_fs_long(buffer+2); /* protection */
95: flags = (int) get_fs_long(buffer+3); /* mapping type */
96: fd = (int) get_fs_long(buffer+4); /* object to map */
97: off = (unsigned long) get_fs_long(buffer+5); /* offset in object */
98:
99: if (fd >= NR_OPEN || fd < 0 || !(file = current->filp[fd]))
100: return (caddr_t) -EBADF;
101: if (addr > TASK_SIZE || (addr+(unsigned long) len) > TASK_SIZE)
102: return (caddr_t) -EINVAL;
103: inode = file->f_inode;
104:
105: /*
106: * do simple checking here so the lower-level routines won't have
107: * to. we assume access permissions have been handled by the open
108: * of the memory object, so we don't do any here.
109: */
110:
111: switch (flags & MAP_TYPE) {
112: case MAP_SHARED:
113: if ((prot & PROT_WRITE) && !(file->f_mode & 2))
114: return (caddr_t)-EINVAL;
115: /* fall through */
116: case MAP_PRIVATE:
117: if (!(file->f_mode & 1))
118: return (caddr_t)-EINVAL;
119: break;
120:
121: default:
122: return (caddr_t)-EINVAL;
123: }
124:
125: /*
126: * obtain the address to map to. we verify (or select) it and ensure
127: * that it represents a valid section of the address space. we assume
128: * that if PROT_EXEC is specified this should be in the code segment.
129: */
130: if (prot & PROT_EXEC) {
131: base = get_base(current->ldt[1]); /* cs */
132: limit = get_limit(0x0f); /* cs limit */
133: } else {
134: base = get_base(current->ldt[2]); /* ds */
135: limit = get_limit(0x17); /* ds limit */
136: }
137:
138: if (flags & MAP_FIXED) {
139: /*
140: * if MAP_FIXED is specified, we have to map exactly at this
141: * address. it must be page aligned and not ambiguous.
142: */
143: if ((addr & 0xfff) || addr > 0x7fffffff || addr == 0 ||
144: (off & 0xfff))
145: return (caddr_t)-EINVAL;
146: if (addr + len > limit)
147: return (caddr_t)-ENOMEM;
148: } else {
149: /*
150: * we're given a hint as to where to put the address.
151: * that we still need to search for a range of pages which
152: * are not mapped and which won't impact the stack or data
153: * segment.
154: * in linux, we only have a code segment and data segment.
155: * since data grows up and stack grows down, we're sort of
156: * stuck. placing above the data will break malloc, below
157: * the stack will cause stack overflow. because of this
158: * we don't allow nonspecified mappings...
159: */
160: return (caddr_t)-ENOMEM;
161: }
162:
163: /*
164: * determine the object being mapped and call the appropriate
165: * specific mapper. the address has already been validated, but
166: * not unmapped
167: */
168: if (S_ISCHR(inode->i_mode))
169: addr = (unsigned long)mmap_chr(base + addr, len, prot, flags,
170: inode, off);
171: else
172: addr = (unsigned long)-ENODEV;
173: if ((long)addr > 0)
174: addr -= base;
175:
176: return (caddr_t)addr;
177: }
178:
179: int sys_munmap(unsigned long addr, size_t len)
180: {
181: unsigned long base, limit;
182:
183: base = get_base(current->ldt[2]); /* map into ds */
184: limit = get_limit(0x17); /* ds limit */
185:
186: if ((addr & 0xfff) || addr > 0x7fffffff || addr == 0 ||
187: addr + len > limit)
188: return -EINVAL;
189: if (unmap_page_range(base + addr, len))
190: return -EAGAIN; /* should never happen */
191: return 0;
192: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.