Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / arch / sparc / kernel / sys_sparc.c
blob498fdb26f4bf0217c9928a33857f0236a491c1b5
1 /* $Id: sys_sparc.c,v 1.67 2000/11/30 08:37:31 anton Exp $
2 * linux/arch/sparc/kernel/sys_sparc.c
4 * This file contains various random system calls that
5 * have a non-standard calling sequence on the Linux/sparc
6 * platform.
7 */
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/fs.h>
14 #include <linux/file.h>
15 #include <linux/sem.h>
16 #include <linux/msg.h>
17 #include <linux/shm.h>
18 #include <linux/stat.h>
19 #include <linux/mman.h>
20 #include <linux/utsname.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
24 #include <asm/uaccess.h>
25 #include <asm/ipc.h>
27 /* #define DEBUG_UNIMP_SYSCALL */
29 /* XXX Make this per-binary type, this way we can detect the type of
30 * XXX a binary. Every Sparc executable calls this very early on.
32 asmlinkage unsigned long sys_getpagesize(void)
34 return PAGE_SIZE; /* Possibly older binaries want 8192 on sun4's? */
37 #define COLOUR_ALIGN(addr) (((addr)+SHMLBA-1)&~(SHMLBA-1))
39 unsigned long get_unmapped_area(unsigned long addr, unsigned long len)
41 struct vm_area_struct * vmm;
43 /* See asm-sparc/uaccess.h */
44 if (len > TASK_SIZE - PAGE_SIZE)
45 return 0;
46 if (ARCH_SUN4C_SUN4 && len > 0x20000000)
47 return 0;
48 if (!addr)
49 addr = TASK_UNMAPPED_BASE;
51 if (current->thread.flags & SPARC_FLAG_MMAPSHARED)
52 addr = COLOUR_ALIGN(addr);
53 else
54 addr = PAGE_ALIGN(addr);
56 for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
57 /* At this point: (!vmm || addr < vmm->vm_end). */
58 if (ARCH_SUN4C_SUN4 && addr < 0xe0000000 && 0x20000000 - len < addr) {
59 addr = PAGE_OFFSET;
60 vmm = find_vma(current->mm, PAGE_OFFSET);
62 if (TASK_SIZE - PAGE_SIZE - len < addr)
63 return 0;
64 if (!vmm || addr + len <= vmm->vm_start)
65 return addr;
66 addr = vmm->vm_end;
67 if (current->thread.flags & SPARC_FLAG_MMAPSHARED)
68 addr = COLOUR_ALIGN(addr);
72 extern asmlinkage unsigned long sys_brk(unsigned long brk);
74 asmlinkage unsigned long sparc_brk(unsigned long brk)
76 if(ARCH_SUN4C_SUN4) {
77 if ((brk & 0xe0000000) != (current->mm->brk & 0xe0000000))
78 return current->mm->brk;
80 return sys_brk(brk);
84 * sys_pipe() is the normal C calling standard for creating
85 * a pipe. It's not the way unix traditionally does this, though.
87 asmlinkage int sparc_pipe(struct pt_regs *regs)
89 int fd[2];
90 int error;
92 error = do_pipe(fd);
93 if (error)
94 goto out;
95 regs->u_regs[UREG_I1] = fd[1];
96 error = fd[0];
97 out:
98 return error;
102 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
104 * This is really horribly ugly.
107 asmlinkage int sys_ipc (uint call, int first, int second, int third, void *ptr, long fifth)
109 int version, err;
111 version = call >> 16; /* hack for backward compatibility */
112 call &= 0xffff;
114 if (call <= SEMCTL)
115 switch (call) {
116 case SEMOP:
117 err = sys_semop (first, (struct sembuf *)ptr, second);
118 goto out;
119 case SEMGET:
120 err = sys_semget (first, second, third);
121 goto out;
122 case SEMCTL: {
123 union semun fourth;
124 err = -EINVAL;
125 if (!ptr)
126 goto out;
127 err = -EFAULT;
128 if(get_user(fourth.__pad, (void **)ptr))
129 goto out;
130 err = sys_semctl (first, second, third, fourth);
131 goto out;
133 default:
134 err = -EINVAL;
135 goto out;
137 if (call <= MSGCTL)
138 switch (call) {
139 case MSGSND:
140 err = sys_msgsnd (first, (struct msgbuf *) ptr,
141 second, third);
142 goto out;
143 case MSGRCV:
144 switch (version) {
145 case 0: {
146 struct ipc_kludge tmp;
147 err = -EINVAL;
148 if (!ptr)
149 goto out;
150 err = -EFAULT;
151 if(copy_from_user(&tmp,(struct ipc_kludge *) ptr, sizeof (tmp)))
152 goto out;
153 err = sys_msgrcv (first, tmp.msgp, second, tmp.msgtyp, third);
154 goto out;
156 case 1: default:
157 err = sys_msgrcv (first, (struct msgbuf *) ptr, second, fifth, third);
158 goto out;
160 case MSGGET:
161 err = sys_msgget ((key_t) first, second);
162 goto out;
163 case MSGCTL:
164 err = sys_msgctl (first, second, (struct msqid_ds *) ptr);
165 goto out;
166 default:
167 err = -EINVAL;
168 goto out;
170 if (call <= SHMCTL)
171 switch (call) {
172 case SHMAT:
173 switch (version) {
174 case 0: default: {
175 ulong raddr;
176 err = sys_shmat (first, (char *) ptr, second, &raddr);
177 if (err)
178 goto out;
179 err = -EFAULT;
180 if(put_user (raddr, (ulong *) third))
181 goto out;
182 err = 0;
183 goto out;
185 case 1: /* iBCS2 emulator entry point */
186 err = sys_shmat (first, (char *) ptr, second, (ulong *) third);
187 goto out;
189 case SHMDT:
190 err = sys_shmdt ((char *)ptr);
191 goto out;
192 case SHMGET:
193 err = sys_shmget (first, second, third);
194 goto out;
195 case SHMCTL:
196 err = sys_shmctl (first, second, (struct shmid_ds *) ptr);
197 goto out;
198 default:
199 err = -EINVAL;
200 goto out;
202 else
203 err = -EINVAL;
204 out:
205 return err;
208 /* Linux version of mmap */
209 static unsigned long do_mmap2(unsigned long addr, unsigned long len,
210 unsigned long prot, unsigned long flags, unsigned long fd,
211 unsigned long pgoff)
213 struct file * file = NULL;
214 unsigned long retval = -EBADF;
216 if (!(flags & MAP_ANONYMOUS)) {
217 file = fget(fd);
218 if (!file)
219 goto out;
222 retval = -EINVAL;
223 len = PAGE_ALIGN(len);
224 if (ARCH_SUN4C_SUN4 &&
225 (len > 0x20000000 ||
226 ((flags & MAP_FIXED) &&
227 addr < 0xe0000000 && addr + len > 0x20000000)))
228 goto out_putf;
230 /* See asm-sparc/uaccess.h */
231 if (len > TASK_SIZE - PAGE_SIZE || addr + len > TASK_SIZE - PAGE_SIZE)
232 goto out_putf;
234 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
236 if (flags & MAP_SHARED)
237 current->thread.flags |= SPARC_FLAG_MMAPSHARED;
239 down(&current->mm->mmap_sem);
240 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
241 up(&current->mm->mmap_sem);
243 current->thread.flags &= ~(SPARC_FLAG_MMAPSHARED);
245 out_putf:
246 if (file)
247 fput(file);
248 out:
249 return retval;
252 asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
253 unsigned long prot, unsigned long flags, unsigned long fd,
254 unsigned long pgoff)
256 /* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
257 we have. */
258 return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT - 12));
261 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
262 unsigned long prot, unsigned long flags, unsigned long fd,
263 unsigned long off)
265 return do_mmap2(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
268 extern unsigned long do_mremap(unsigned long addr,
269 unsigned long old_len, unsigned long new_len,
270 unsigned long flags, unsigned long new_addr);
272 asmlinkage unsigned long sparc_mremap(unsigned long addr,
273 unsigned long old_len, unsigned long new_len,
274 unsigned long flags, unsigned long new_addr)
276 struct vm_area_struct *vma;
277 unsigned long ret = -EINVAL;
278 if (ARCH_SUN4C_SUN4) {
279 if (old_len > 0x20000000 || new_len > 0x20000000)
280 goto out;
281 if (addr < 0xe0000000 && addr + old_len > 0x20000000)
282 goto out;
284 if (old_len > TASK_SIZE - PAGE_SIZE ||
285 new_len > TASK_SIZE - PAGE_SIZE)
286 goto out;
287 down(&current->mm->mmap_sem);
288 vma = find_vma(current->mm, addr);
289 if (vma && (vma->vm_flags & VM_SHARED))
290 current->thread.flags |= SPARC_FLAG_MMAPSHARED;
291 if (flags & MREMAP_FIXED) {
292 if (ARCH_SUN4C_SUN4 &&
293 new_addr < 0xe0000000 &&
294 new_addr + new_len > 0x20000000)
295 goto out_sem;
296 if (new_addr + new_len > TASK_SIZE - PAGE_SIZE)
297 goto out_sem;
298 } else if ((ARCH_SUN4C_SUN4 && addr < 0xe0000000 &&
299 addr + new_len > 0x20000000) ||
300 addr + new_len > TASK_SIZE - PAGE_SIZE) {
301 ret = -ENOMEM;
302 if (!(flags & MREMAP_MAYMOVE))
303 goto out_sem;
304 new_addr = get_unmapped_area (addr, new_len);
305 if (!new_addr)
306 goto out_sem;
307 flags |= MREMAP_FIXED;
309 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
310 out_sem:
311 current->thread.flags &= ~(SPARC_FLAG_MMAPSHARED);
312 up(&current->mm->mmap_sem);
313 out:
314 return ret;
317 /* we come to here via sys_nis_syscall so it can setup the regs argument */
318 asmlinkage unsigned long
319 c_sys_nis_syscall (struct pt_regs *regs)
321 static int count = 0;
323 if (count++ > 5) return -ENOSYS;
324 printk ("%s[%d]: Unimplemented SPARC system call %d\n", current->comm, current->pid, (int)regs->u_regs[1]);
325 #ifdef DEBUG_UNIMP_SYSCALL
326 show_regs (regs);
327 #endif
328 return -ENOSYS;
331 /* #define DEBUG_SPARC_BREAKPOINT */
333 asmlinkage void
334 sparc_breakpoint (struct pt_regs *regs)
336 siginfo_t info;
338 lock_kernel();
339 #ifdef DEBUG_SPARC_BREAKPOINT
340 printk ("TRAP: Entering kernel PC=%x, nPC=%x\n", regs->pc, regs->npc);
341 #endif
342 info.si_signo = SIGTRAP;
343 info.si_errno = 0;
344 info.si_code = TRAP_BRKPT;
345 info.si_addr = (void *)regs->pc;
346 info.si_trapno = 0;
347 force_sig_info(SIGTRAP, &info, current);
349 #ifdef DEBUG_SPARC_BREAKPOINT
350 printk ("TRAP: Returning to space: PC=%x nPC=%x\n", regs->pc, regs->npc);
351 #endif
352 unlock_kernel();
355 asmlinkage int
356 sparc_sigaction (int sig, const struct old_sigaction *act,
357 struct old_sigaction *oact)
359 struct k_sigaction new_ka, old_ka;
360 int ret;
362 if (sig < 0) {
363 current->thread.new_signal = 1;
364 sig = -sig;
367 if (act) {
368 unsigned long mask;
370 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
371 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
372 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
373 return -EFAULT;
374 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
375 __get_user(mask, &act->sa_mask);
376 siginitset(&new_ka.sa.sa_mask, mask);
377 new_ka.ka_restorer = NULL;
380 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
382 if (!ret && oact) {
383 /* In the clone() case we could copy half consistant
384 * state to the user, however this could sleep and
385 * deadlock us if we held the signal lock on SMP. So for
386 * now I take the easy way out and do no locking.
388 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
389 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
390 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
391 return -EFAULT;
392 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
393 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
396 return ret;
399 asmlinkage int
400 sys_rt_sigaction(int sig, const struct sigaction *act, struct sigaction *oact,
401 void *restorer, size_t sigsetsize)
403 struct k_sigaction new_ka, old_ka;
404 int ret;
406 /* XXX: Don't preclude handling different sized sigset_t's. */
407 if (sigsetsize != sizeof(sigset_t))
408 return -EINVAL;
410 /* All tasks which use RT signals (effectively) use
411 * new style signals.
413 current->thread.new_signal = 1;
415 if (act) {
416 new_ka.ka_restorer = restorer;
417 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
418 return -EFAULT;
421 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
423 if (!ret && oact) {
424 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
425 return -EFAULT;
428 return ret;
431 /* Just in case some old old binary calls this. */
432 asmlinkage int sys_pause(void)
434 current->state = TASK_INTERRUPTIBLE;
435 schedule();
436 return -ERESTARTNOHAND;
439 asmlinkage int sys_getdomainname(char *name, int len)
441 int nlen;
442 int err = -EFAULT;
444 down_read(&uts_sem);
446 nlen = strlen(system_utsname.domainname) + 1;
448 if (nlen < len)
449 len = nlen;
450 if(len > __NEW_UTS_LEN)
451 goto done;
452 if(copy_to_user(name, system_utsname.domainname, len))
453 goto done;
454 err = 0;
455 done:
456 up_read(&uts_sem);
457 return err;