1 /*
2  * linux/arch/ppc/kernel/sys_ppc.c
3  *
4  *  PowerPC version
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *
7  * Derived from "arch/i386/kernel/sys_i386.c"
8  * Adapted from the i386 version by Gary Thomas
9  * Modified by Cort Dougan (cort@cs.nmt.edu)
10  * and Paul Mackerras (paulus@cs.anu.edu.au).
11  *
12  * This file contains various random system calls that
13  * have a non-standard calling sequence on the Linux/PPC
14  * platform.
15  *
16  *  This program is free software; you can redistribute it and/or
17  *  modify it under the terms of the GNU General Public License
18  *  as published by the Free Software Foundation; either version
19  *  2 of the License, or (at your option) any later version.
20  *
21  */
22 
23 #include <linux/errno.h>
24 #include <linux/sched.h>
25 #include <linux/mm.h>
26 #include <linux/smp.h>
27 #include <linux/smp_lock.h>
28 #include <linux/sem.h>
29 #include <linux/msg.h>
30 #include <linux/shm.h>
31 #include <linux/stat.h>
32 #include <linux/mman.h>
33 #include <linux/sys.h>
34 #include <linux/ipc.h>
35 #include <linux/utsname.h>
36 #include <linux/file.h>
37 #include <linux/init.h>
38 #include <linux/personality.h>
39 
40 #include <asm/uaccess.h>
41 #include <asm/ipc.h>
42 #include <asm/semaphore.h>
43 #include <asm/ppcdebug.h>
44 
45 #include <asm/time.h>
46 
47 extern unsigned long wall_jiffies;
48 #define USEC_PER_SEC (1000000)
49 
50 void
check_bugs(void)51 check_bugs(void)
52 {
53 }
54 
sys_ioperm(unsigned long from,unsigned long num,int on)55 asmlinkage int sys_ioperm(unsigned long from, unsigned long num, int on)
56 {
57 	return -EIO;
58 }
59 
60 /*
61  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
62  *
63  * This is really horribly ugly.
64  */
65 asmlinkage int
sys_ipc(uint call,int first,int second,long third,void * ptr,long fifth)66 sys_ipc (uint call, int first, int second, long third, void *ptr, long fifth)
67 {
68 	int version, ret;
69 
70 	PPCDBG(PPCDBG_SYS64X, "sys_ipc - entered - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm);
71 
72 	version = call >> 16; /* hack for backward compatibility */
73 	call &= 0xffff;
74 
75 	ret = -ENOSYS;
76 	switch (call) {
77 	case SEMOP:
78 		ret = sys_semop (first, (struct sembuf *)ptr, second);
79 		break;
80 	case SEMGET:
81 		ret = sys_semget (first, second, third);
82 		break;
83 	case SEMCTL: {
84 		union semun fourth;
85 
86 		if (!ptr)
87 			break;
88 		if ((ret = verify_area (VERIFY_READ, ptr, sizeof(long)))
89 		    || (ret = get_user(fourth.__pad, (void **)ptr)))
90 			break;
91 		ret = sys_semctl (first, second, third, fourth);
92 		break;
93 	}
94 	case MSGSND:
95 		ret = sys_msgsnd (first, (struct msgbuf *) ptr, second, third);
96 		break;
97 	case MSGRCV:
98 		switch (version) {
99 		case 0: {
100 			struct ipc_kludge tmp;
101 
102 			if (!ptr)
103 				break;
104 			if ((ret = verify_area (VERIFY_READ, ptr, sizeof(tmp)))
105 			    || (ret = copy_from_user(&tmp,
106 						(struct ipc_kludge *) ptr,
107 						sizeof (tmp))))
108 				break;
109 			ret = sys_msgrcv (first, (struct msgbuf *)tmp.msgp,
110 						second, tmp.msgtyp, third);
111 			break;
112 		}
113 		default:
114 			ret = sys_msgrcv (first, (struct msgbuf *) ptr,
115 					  second, fifth, third);
116 			break;
117 		}
118 		break;
119 	case MSGGET:
120 		ret = sys_msgget ((key_t) first, second);
121 		break;
122 	case MSGCTL:
123 		ret = sys_msgctl (first, second, (struct msqid_ds *) ptr);
124 		break;
125 	case SHMAT:
126 		switch (version) {
127 		default: {
128 			ulong raddr;
129 
130 			if ((ret = verify_area(VERIFY_WRITE, (ulong*) third,
131 					       sizeof(ulong))))
132 				break;
133 			ret = sys_shmat (first, (char *) ptr, second, &raddr);
134 			if (ret)
135 				break;
136 			ret = put_user (raddr, (ulong *) third);
137 			break;
138 		}
139 		case 1:	/* iBCS2 emulator entry point */
140 			if (!segment_eq(get_fs(), get_ds()))
141 				break;
142 			ret = sys_shmat (first, (char *) ptr, second,
143 					 (ulong *) third);
144 			break;
145 		}
146 		break;
147 	case SHMDT:
148 		ret = sys_shmdt ((char *)ptr);
149 		break;
150 	case SHMGET:
151 		ret = sys_shmget (first, second, third);
152 		break;
153 	case SHMCTL:
154 		ret = sys_shmctl (first, second, (struct shmid_ds *) ptr);
155 		break;
156 	}
157 
158 	PPCDBG(PPCDBG_SYS64X, "sys_ipc - exited - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm);
159 	return ret;
160 }
161 
162 /*
163  * sys_pipe() is the normal C calling standard for creating
164  * a pipe. It's not the way unix traditionally does this, though.
165  */
sys_pipe(int * fildes)166 asmlinkage int sys_pipe(int *fildes)
167 {
168 	int fd[2];
169 	int error;
170 
171 	PPCDBG(PPCDBG_SYS64X, "sys_pipe - entered - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm);
172 
173 	error = do_pipe(fd);
174 	if (!error) {
175 		if (copy_to_user(fildes, fd, 2*sizeof(int)))
176 			error = -EFAULT;
177 	}
178 
179 	PPCDBG(PPCDBG_SYS64X, "sys_pipe - exited - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm);
180 	return error;
181 }
182 
sys_mmap(unsigned long addr,size_t len,unsigned long prot,unsigned long flags,unsigned long fd,off_t offset)183 asmlinkage unsigned long sys_mmap(unsigned long addr, size_t len,
184 				  unsigned long prot, unsigned long flags,
185 				  unsigned long fd, off_t offset)
186 {
187 	struct file * file = NULL;
188 	unsigned long ret = -EBADF;
189 
190 	PPCDBG(PPCDBG_SYS64X, "sys_mmap - entered - addr=%lx, len=%lx - pid=%ld, comm=%s \n", addr, len, current->pid, current->comm);
191 
192 	if (!(flags & MAP_ANONYMOUS)) {
193 		if (!(file = fget(fd)))
194 			goto out;
195 	}
196 
197 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
198 	down_write(&current->mm->mmap_sem);
199 	ret = do_mmap(file, addr, len, prot, flags, offset);
200 	up_write(&current->mm->mmap_sem);
201 	if (file)
202 		fput(file);
203 
204 out:
205 
206 	PPCDBG(PPCDBG_SYS64X, "sys_mmap - exited - ret=%x \n", ret);
207 
208 	return ret;
209 }
210 
sys_pause(void)211 asmlinkage int sys_pause(void)
212 {
213 
214 	PPCDBG(PPCDBG_SYS64X, "sys_pause - entered - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm);
215 	current->state = TASK_INTERRUPTIBLE;
216 	schedule();
217 
218 	PPCDBG(PPCDBG_SYS64X, "sys_pause - exited - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm);
219 	return -ERESTARTNOHAND;
220 }
221 
set_fakeppc(char * str)222 static int __init set_fakeppc(char *str)
223 {
224 	if (*str)
225 		return 0;
226 	init_task.personality = PER_LINUX32;
227 	return 1;
228 }
229 __setup("fakeppc", set_fakeppc);
230 
sys_uname(struct old_utsname * name)231 asmlinkage int sys_uname(struct old_utsname * name)
232 {
233 	int err = -EFAULT;
234 
235 	PPCDBG(PPCDBG_SYS64X, "sys_uname - entered - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm);
236 
237 	down_read(&uts_sem);
238 	if (name && !copy_to_user(name, &system_utsname, sizeof (*name)))
239 		err = 0;
240 	up_read(&uts_sem);
241 
242 	PPCDBG(PPCDBG_SYS64X, "sys_uname - exited - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm);
243 	return err;
244 }
245 
sys_olduname(struct oldold_utsname * name)246 asmlinkage int sys_olduname(struct oldold_utsname * name)
247 {
248 	int error;
249 
250 	PPCDBG(PPCDBG_SYS64X, "sys_olduname - entered - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm);
251 
252 	if (!name)
253 		return -EFAULT;
254 	if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
255 		return -EFAULT;
256 
257 	down_read(&uts_sem);
258 	error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
259 	error -= __put_user(0,name->sysname+__OLD_UTS_LEN);
260 	error -= __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
261 	error -= __put_user(0,name->nodename+__OLD_UTS_LEN);
262 	error -= __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN);
263 	error -= __put_user(0,name->release+__OLD_UTS_LEN);
264 	error -= __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN);
265 	error -= __put_user(0,name->version+__OLD_UTS_LEN);
266 	error -= __copy_to_user(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
267 	error = __put_user(0,name->machine+__OLD_UTS_LEN);
268 	up_read(&uts_sem);
269 
270 	error = error ? -EFAULT : 0;
271 
272 	PPCDBG(PPCDBG_SYS64X, "sys_olduname - exited - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm);
273 	return error;
274 }
275 
sys64_time(time_t * tloc)276 asmlinkage time_t sys64_time(time_t* tloc)
277 {
278 	time_t secs;
279 	time_t usecs;
280 
281 	long tb_delta = tb_ticks_since(tb_last_stamp);
282 	tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
283 
284 	secs  = xtime.tv_sec;
285 	usecs = xtime.tv_usec + tb_delta / tb_ticks_per_usec;
286 	while (usecs >= USEC_PER_SEC) {
287 		++secs;
288 		usecs -= USEC_PER_SEC;
289 	}
290 
291 	if (tloc) {
292 		if (put_user(secs,tloc))
293 			secs = -EFAULT;
294 	}
295 
296 	return secs;
297 }
298