sys_pipe(): fix file descriptor leaks
[linux-2.6/mini2440.git] / arch / m32r / kernel / sys_m32r.c
blob319c79720b8a2ba3860ff35fad5a20b2552210cc
1 /*
2 * linux/arch/m32r/kernel/sys_m32r.c
4 * This file contains various random system calls that
5 * have a non-standard calling sequence on the Linux/M32R platform.
7 * Taken from i386 version.
8 */
10 #include <linux/errno.h>
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/fs.h>
14 #include <linux/smp.h>
15 #include <linux/sem.h>
16 #include <linux/msg.h>
17 #include <linux/shm.h>
18 #include <linux/stat.h>
19 #include <linux/syscalls.h>
20 #include <linux/mman.h>
21 #include <linux/file.h>
22 #include <linux/utsname.h>
23 #include <linux/ipc.h>
25 #include <asm/uaccess.h>
26 #include <asm/cachectl.h>
27 #include <asm/cacheflush.h>
28 #include <asm/syscall.h>
29 #include <asm/unistd.h>
32 * sys_tas() - test-and-set
34 asmlinkage int sys_tas(int __user *addr)
36 int oldval;
38 if (!access_ok(VERIFY_WRITE, addr, sizeof (int)))
39 return -EFAULT;
41 /* atomic operation:
42 * oldval = *addr; *addr = 1;
44 __asm__ __volatile__ (
45 DCACHE_CLEAR("%0", "r4", "%1")
46 " .fillinsn\n"
47 "1:\n"
48 " lock %0, @%1 -> unlock %2, @%1\n"
49 "2:\n"
50 /* NOTE:
51 * The m32r processor can accept interrupts only
52 * at the 32-bit instruction boundary.
53 * So, in the above code, the "unlock" instruction
54 * can be executed continuously after the "lock"
55 * instruction execution without any interruptions.
57 ".section .fixup,\"ax\"\n"
58 " .balign 4\n"
59 "3: ldi %0, #%3\n"
60 " seth r14, #high(2b)\n"
61 " or3 r14, r14, #low(2b)\n"
62 " jmp r14\n"
63 ".previous\n"
64 ".section __ex_table,\"a\"\n"
65 " .balign 4\n"
66 " .long 1b,3b\n"
67 ".previous\n"
68 : "=&r" (oldval)
69 : "r" (addr), "r" (1), "i"(-EFAULT)
70 : "r14", "memory"
71 #ifdef CONFIG_CHIP_M32700_TS1
72 , "r4"
73 #endif /* CONFIG_CHIP_M32700_TS1 */
76 return oldval;
80 * sys_pipe() is the normal C calling standard for creating
81 * a pipe. It's not the way Unix traditionally does this, though.
83 asmlinkage int
84 sys_pipe(unsigned long r0, unsigned long r1, unsigned long r2,
85 unsigned long r3, unsigned long r4, unsigned long r5,
86 unsigned long r6, struct pt_regs regs)
88 int fd[2];
89 int error;
91 error = do_pipe(fd);
92 if (!error) {
93 if (copy_to_user((void __user *)r0, fd, 2*sizeof(int))) {
94 sys_close(fd[0]);
95 sys_close(fd[1]);
96 error = -EFAULT;
99 return error;
102 asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
103 unsigned long prot, unsigned long flags,
104 unsigned long fd, unsigned long pgoff)
106 int error = -EBADF;
107 struct file *file = NULL;
109 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
110 if (!(flags & MAP_ANONYMOUS)) {
111 file = fget(fd);
112 if (!file)
113 goto out;
116 down_write(&current->mm->mmap_sem);
117 error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
118 up_write(&current->mm->mmap_sem);
120 if (file)
121 fput(file);
122 out:
123 return error;
127 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
129 * This is really horribly ugly.
131 asmlinkage int sys_ipc(uint call, int first, int second,
132 int third, void __user *ptr, long fifth)
134 int version, ret;
136 version = call >> 16; /* hack for backward compatibility */
137 call &= 0xffff;
139 switch (call) {
140 case SEMOP:
141 return sys_semtimedop(first, (struct sembuf __user *)ptr,
142 second, NULL);
143 case SEMTIMEDOP:
144 return sys_semtimedop(first, (struct sembuf __user *)ptr,
145 second, (const struct timespec __user *)fifth);
146 case SEMGET:
147 return sys_semget (first, second, third);
148 case SEMCTL: {
149 union semun fourth;
150 if (!ptr)
151 return -EINVAL;
152 if (get_user(fourth.__pad, (void __user * __user *) ptr))
153 return -EFAULT;
154 return sys_semctl (first, second, third, fourth);
157 case MSGSND:
158 return sys_msgsnd (first, (struct msgbuf __user *) ptr,
159 second, third);
160 case MSGRCV:
161 switch (version) {
162 case 0: {
163 struct ipc_kludge tmp;
164 if (!ptr)
165 return -EINVAL;
167 if (copy_from_user(&tmp,
168 (struct ipc_kludge __user *) ptr,
169 sizeof (tmp)))
170 return -EFAULT;
171 return sys_msgrcv (first, tmp.msgp, second,
172 tmp.msgtyp, third);
174 default:
175 return sys_msgrcv (first,
176 (struct msgbuf __user *) ptr,
177 second, fifth, third);
179 case MSGGET:
180 return sys_msgget ((key_t) first, second);
181 case MSGCTL:
182 return sys_msgctl (first, second,
183 (struct msqid_ds __user *) ptr);
184 case SHMAT: {
185 ulong raddr;
187 if (!access_ok(VERIFY_WRITE, (ulong __user *) third,
188 sizeof(ulong)))
189 return -EFAULT;
190 ret = do_shmat (first, (char __user *) ptr, second, &raddr);
191 if (ret)
192 return ret;
193 return put_user (raddr, (ulong __user *) third);
195 case SHMDT:
196 return sys_shmdt ((char __user *)ptr);
197 case SHMGET:
198 return sys_shmget (first, second, third);
199 case SHMCTL:
200 return sys_shmctl (first, second,
201 (struct shmid_ds __user *) ptr);
202 default:
203 return -ENOSYS;
207 asmlinkage int sys_uname(struct old_utsname __user * name)
209 int err;
210 if (!name)
211 return -EFAULT;
212 down_read(&uts_sem);
213 err = copy_to_user(name, utsname(), sizeof (*name));
214 up_read(&uts_sem);
215 return err?-EFAULT:0;
218 asmlinkage int sys_cacheflush(void *addr, int bytes, int cache)
220 /* This should flush more selectively ... */
221 _flush_cache_all();
222 return 0;
225 asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
227 /* Not implemented yet. */
228 return -ENOSYS;
232 * Do a system call from kernel instead of calling sys_execve so we
233 * end up with proper pt_regs.
235 int kernel_execve(const char *filename, char *const argv[], char *const envp[])
237 register long __scno __asm__ ("r7") = __NR_execve;
238 register long __arg3 __asm__ ("r2") = (long)(envp);
239 register long __arg2 __asm__ ("r1") = (long)(argv);
240 register long __res __asm__ ("r0") = (long)(filename);
241 __asm__ __volatile__ (
242 "trap #" SYSCALL_VECTOR "|| nop"
243 : "=r" (__res)
244 : "r" (__scno), "0" (__res), "r" (__arg2),
245 "r" (__arg3)
246 : "memory");
247 return __res;