Merge with /home/shaggy/git/linus-clean/
[linux-2.6] / arch / um / kernel / skas / mem_user.c
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <signal.h>
7 #include <errno.h>
8 #include <sys/mman.h>
9 #include <sys/wait.h>
10 #include <asm/page.h>
11 #include <asm/unistd.h>
12 #include "mem_user.h"
13 #include "mem.h"
14 #include "mm_id.h"
15 #include "user.h"
16 #include "os.h"
17 #include "proc_mm.h"
18 #include "ptrace_user.h"
19 #include "user_util.h"
20 #include "kern_util.h"
21 #include "task.h"
22 #include "registers.h"
23 #include "uml-config.h"
24 #include "sysdep/ptrace.h"
25 #include "sysdep/stub.h"
26 #include "skas.h"
27
28 extern unsigned long syscall_stub, __syscall_stub_start;
29
30 extern void wait_stub_done(int pid, int sig, char * fname);
31
32 static long run_syscall_stub(struct mm_id * mm_idp, int syscall,
33                              unsigned long *args)
34 {
35         int n, pid = mm_idp->u.pid;
36         unsigned long regs[MAX_REG_NR];
37
38         get_safe_registers(regs);
39         regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE +
40                 ((unsigned long) &syscall_stub -
41                  (unsigned long) &__syscall_stub_start);
42         /* XXX Don't have a define for starting a syscall */
43         regs[REGS_SYSCALL_NR] = syscall;
44         regs[REGS_SYSCALL_ARG1] = args[0];
45         regs[REGS_SYSCALL_ARG2] = args[1];
46         regs[REGS_SYSCALL_ARG3] = args[2];
47         regs[REGS_SYSCALL_ARG4] = args[3];
48         regs[REGS_SYSCALL_ARG5] = args[4];
49         regs[REGS_SYSCALL_ARG6] = args[5];
50         n = ptrace_setregs(pid, regs);
51         if(n < 0){
52                 printk("run_syscall_stub : PTRACE_SETREGS failed, "
53                        "errno = %d\n", n);
54                 return(n);
55         }
56
57         wait_stub_done(pid, 0, "run_syscall_stub");
58
59         return(*((unsigned long *) mm_idp->stack));
60 }
61
62 int map(struct mm_id *mm_idp, unsigned long virt, unsigned long len,
63         int r, int w, int x, int phys_fd, unsigned long long offset)
64 {
65         int prot, n;
66
67         prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
68                 (x ? PROT_EXEC : 0);
69
70         if(proc_mm){
71                 struct proc_mm_op map;
72                 int fd = mm_idp->u.mm_fd;
73                 map = ((struct proc_mm_op) { .op        = MM_MMAP,
74                                              .u         =
75                                              { .mmap    =
76                                                { .addr  = virt,
77                                                  .len   = len,
78                                                  .prot  = prot,
79                                                  .flags = MAP_SHARED |
80                                                  MAP_FIXED,
81                                                  .fd    = phys_fd,
82                                                  .offset= offset
83                                                } } } );
84                 n = os_write_file(fd, &map, sizeof(map));
85                 if(n != sizeof(map))
86                         printk("map : /proc/mm map failed, err = %d\n", -n);
87         }
88         else {
89                 long res;
90                 unsigned long args[] = { virt, len, prot,
91                                          MAP_SHARED | MAP_FIXED, phys_fd,
92                                          MMAP_OFFSET(offset) };
93
94                 res = run_syscall_stub(mm_idp, STUB_MMAP_NR, args);
95                 if((void *) res == MAP_FAILED)
96                         printk("mmap stub failed, errno = %d\n", res);
97         }
98
99         return 0;
100 }
101
102 int unmap(struct mm_id *mm_idp, void *addr, unsigned long len)
103 {
104         int n;
105
106         if(proc_mm){
107                 struct proc_mm_op unmap;
108                 int fd = mm_idp->u.mm_fd;
109                 unmap = ((struct proc_mm_op) { .op      = MM_MUNMAP,
110                                                .u       =
111                                                { .munmap        =
112                                                  { .addr        =
113                                                    (unsigned long) addr,
114                                                    .len         = len } } } );
115                 n = os_write_file(fd, &unmap, sizeof(unmap));
116                 if(n != sizeof(unmap)) {
117                         if(n < 0)
118                                 return(n);
119                         else if(n > 0)
120                                 return(-EIO);
121                 }
122         }
123         else {
124                 int res;
125                 unsigned long args[] = { (unsigned long) addr, len, 0, 0, 0,
126                                          0 };
127
128                 res = run_syscall_stub(mm_idp, __NR_munmap, args);
129                 if(res < 0)
130                         printk("munmap stub failed, errno = %d\n", res);
131         }
132
133         return(0);
134 }
135
136 int protect(struct mm_id *mm_idp, unsigned long addr, unsigned long len,
137             int r, int w, int x)
138 {
139         struct proc_mm_op protect;
140         int prot, n;
141
142         prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
143                 (x ? PROT_EXEC : 0);
144
145         if(proc_mm){
146                 int fd = mm_idp->u.mm_fd;
147                 protect = ((struct proc_mm_op) { .op    = MM_MPROTECT,
148                                                  .u     =
149                                                  { .mprotect    =
150                                                    { .addr      =
151                                                      (unsigned long) addr,
152                                                      .len       = len,
153                                                      .prot      = prot } } } );
154
155                 n = os_write_file(fd, &protect, sizeof(protect));
156                 if(n != sizeof(protect))
157                         panic("protect failed, err = %d", -n);
158         }
159         else {
160                 int res;
161                 unsigned long args[] = { addr, len, prot, 0, 0, 0 };
162
163                 res = run_syscall_stub(mm_idp, __NR_mprotect, args);
164                 if(res < 0)
165                         panic("mprotect stub failed, errno = %d\n", res);
166         }
167
168         return(0);
169 }
170
171 void before_mem_skas(unsigned long unused)
172 {
173 }