File: syscall.c

package info (click to toggle)
kernel-source-2.0.32 2.0.32-5
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 29,648 kB
  • ctags: 86,850
  • sloc: ansic: 542,141; asm: 26,201; makefile: 3,423; sh: 1,195; perl: 727; tcl: 408; cpp: 277; lisp: 211; awk: 134
file content (225 lines) | stat: -rw-r--r-- 4,905 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * MIPS specific syscall handling functions and syscalls
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 1995 by Ralf Baechle
 */
#include <linux/linkage.h>
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/sched.h>
#include <linux/unistd.h>
#include <asm/ptrace.h>
#include <asm/segment.h>
#include <asm/signal.h>

extern asmlinkage void syscall_trace(void);
typedef asmlinkage int (*syscall_t)(void *a0,...);
extern asmlinkage int do_syscalls(struct pt_regs *regs, syscall_t fun,
                                  int narg);
extern syscall_t sys_call_table[];
extern unsigned char sys_narg_table[];

asmlinkage int sys_pipe(struct pt_regs *regs)
{
	int fd[2];
	int error;

	error = do_pipe(fd);
	if (error)
		return error;
	regs->reg2 = fd[0];
	regs->reg3 = fd[1];
	return 0;
}

asmlinkage unsigned long sys_mmap(unsigned long addr, size_t len, int prot,
                                  int flags, int fd, off_t offset)
{
	struct file * file = NULL;

	if (flags & MAP_RENAME) {
		if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
			return -EBADF;
	}
	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);

	return do_mmap(file, addr, len, prot, flags, offset);
}

asmlinkage int sys_idle(void)
{
	if (current->pid != 0)
		return -EPERM;

	/* endless idle loop with no priority at all */
	current->counter = -100;
	for (;;) {
		/*
		 * R4[26]00 have wait, R4[04]00 don't.
		 */
		if (wait_available && !need_resched)
			__asm__(".set\tmips3\n\t"
				"wait\n\t"
				".set\tmips0\n\t");
		schedule();
	}
}

asmlinkage int sys_fork(struct pt_regs *regs)
{
	return do_fork(SIGCHLD, regs->reg29, regs);
}

asmlinkage int sys_clone(struct pt_regs *regs)
{
	unsigned long clone_flags;
	unsigned long newsp;

	clone_flags = regs->reg4;
	newsp = regs->reg5;
	if (!newsp)
		newsp = regs->reg29;
	return do_fork(clone_flags, newsp, regs);
}

/*
 * sys_execve() executes a new program.
 */
asmlinkage int sys_execve(struct pt_regs *regs)
{
	int error;
	char * filename;

	error = getname((char *) regs->reg4, &filename);
	if (error)
		return error;
	error = do_execve(filename, (char **) regs->reg5,
	                  (char **) regs->reg6, regs);
	putname(filename);
	return error;
}

/*
 * Do the indirect syscall syscall.
 */
asmlinkage int sys_syscall(unsigned long a0, unsigned long a1, unsigned long a2,
                           unsigned long a3, unsigned long a4, unsigned long a5,
                           unsigned long a6)
{
	syscall_t syscall;

	if (a0 > __NR_Linux + __NR_Linux_syscalls)
		return -ENOSYS;

	syscall = sys_call_table[a0];
	/*
	 * Prevent stack overflow by recursive
	 * syscall(__NR_syscall, __NR_syscall,...);
	 */
	if (syscall == (syscall_t) sys_syscall)
		return -EINVAL;

	if (syscall == NULL)
		return -ENOSYS;

	return syscall((void *)a0, a1, a2, a3, a4, a5, a6);
}

void do_sys(struct pt_regs *regs)
{
	unsigned long syscallnr, usp;
	syscall_t syscall;
	int errno, narg;

	/*
	 * Compute the return address;
	 */
	if (regs->cp0_cause & CAUSEF_BD)
	{
		/*
		 * This syscall is in a branch delay slot.  Since we don't do
		 * branch delay slot handling we would get a process trying
		 * to do syscalls ever and ever again.  So better zap it.
		 */
		printk("%s: syscall in branch delay slot.\n", current->comm);
		current->sig->action[SIGILL-1].sa_handler = NULL;
		current->blocked &= ~(1<<(SIGILL-1));
		send_sig(SIGILL, current, 1);
		return;
	}
	regs->cp0_epc += 4;

	syscallnr = regs->reg2;
	if (syscallnr > (__NR_Linux + __NR_Linux_syscalls))
		goto illegal_syscall;

	syscall = sys_call_table[syscallnr];
	if (syscall == NULL)
		goto illegal_syscall;

	narg = sys_narg_table[syscallnr];
	if (narg > 4)
	{
		/*
		 * Verify that we can safely get the additional parameters
		 * from the user stack.  Of course I could read the params
		 * from unaligned addresses ...  Consider this a programming
		 * course caliber .45.
		 */
		usp = regs->reg29;
		if (usp & 3)
		{
			printk("unaligned usp\n");
			send_sig(SIGSEGV, current, 1);
			regs->reg2 = EFAULT;
			regs->reg7 = 1;
			return;
		}
		errno = verify_area(VERIFY_READ, (void *) (usp + 16),
		                    (narg - 4) * sizeof(unsigned long));
		if (errno < 0)
			goto bad_syscall;
	}

	if ((current->flags & PF_TRACESYS) == 0)
	{
		errno = do_syscalls(regs, syscall, narg);
		if (errno < 0 || current->errno)
			goto bad_syscall;

		regs->reg2 = errno;
		regs->reg7 = 0;
	}
	else
	{
		syscall_trace();

		errno = do_syscalls(regs, syscall, narg);
		if (errno < 0 || current->errno)
		{
			regs->reg2 = -errno;
			regs->reg7 = 1;
		}
		else
		{
			regs->reg2 = errno;
			regs->reg7 = 0;
		}

		syscall_trace();
	}
	return;

bad_syscall:
	regs->reg2 = -errno;
	regs->reg7 = 1;
	return;
illegal_syscall:
	regs->reg2 = ENOSYS;
	regs->reg7 = 1;
	return;
}