Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* $Id: signal.c,v 1.5 1997/12/15 15:04:59 jj Exp $
 * signal.c: Signal emulation for Solaris
 *
 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
 */

#include <linux/types.h>
#include <linux/smp_lock.h>

#include <asm/uaccess.h>
#include <asm/svr4.h>
#include <asm/string.h>

#include "conv.h"
#include "signal.h"

#define _S(nr) (1L<<((nr)-1))

#define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))

long linux_to_solaris_signals[] = {
        0,
	SOLARIS_SIGHUP,		SOLARIS_SIGINT,	
	SOLARIS_SIGQUIT,	SOLARIS_SIGILL,
	SOLARIS_SIGTRAP,	SOLARIS_SIGIOT,
	SOLARIS_SIGEMT,		SOLARIS_SIGFPE,
	SOLARIS_SIGKILL,	SOLARIS_SIGBUS,
	SOLARIS_SIGSEGV,	SOLARIS_SIGSYS,
	SOLARIS_SIGPIPE,	SOLARIS_SIGALRM,
	SOLARIS_SIGTERM,	SOLARIS_SIGURG,
	SOLARIS_SIGSTOP,	SOLARIS_SIGTSTP,
	SOLARIS_SIGCONT,	SOLARIS_SIGCLD,
	SOLARIS_SIGTTIN,	SOLARIS_SIGTTOU,
	SOLARIS_SIGPOLL,	SOLARIS_SIGXCPU,
	SOLARIS_SIGXFSZ,	SOLARIS_SIGVTALRM,
	SOLARIS_SIGPROF,	SOLARIS_SIGWINCH,
	SOLARIS_SIGUSR1,	SOLARIS_SIGUSR1,
	SOLARIS_SIGUSR2,	-1,
};

long solaris_to_linux_signals[] = {
        0,
        SIGHUP,		SIGINT,		SIGQUIT,	SIGILL,
        SIGTRAP,	SIGIOT,		SIGEMT,		SIGFPE,
        SIGKILL,	SIGBUS,		SIGSEGV,	SIGSYS,
        SIGPIPE,	SIGALRM,	SIGTERM,	SIGUSR1,
        SIGUSR2,	SIGCHLD,	-1,		SIGWINCH,
        SIGURG,		SIGPOLL,	SIGSTOP,	SIGTSTP,
        SIGCONT,	SIGTTIN,	SIGTTOU,	SIGVTALRM,
        SIGPROF,	SIGXCPU,	SIGXFSZ,        -1,
	-1,		-1,		-1,		-1,
	-1,		-1,		-1,		-1,
	-1,		-1,		-1,		-1,
};

static inline long mapsig(long sig)
{
	if ((unsigned long)sig > SOLARIS_NSIGNALS)
		return -EINVAL;
	return solaris_to_linux_signals[sig];
}

asmlinkage int solaris_kill(int pid, int sig)
{
	int (*sys_kill)(int,int) = 
		(int (*)(int,int))SYS(kill);
	int s = mapsig(sig);
	
	if (s < 0) return s;
	return sys_kill(pid, s);
}

static long sig_handler(int sig, u32 arg, int one_shot)
{
	struct sigaction sa, old;
	int ret;
	mm_segment_t old_fs = get_fs();
	int (*sys_sigaction)(int,struct sigaction *,struct sigaction *) = 
		(int (*)(int,struct sigaction *,struct sigaction *))SYS(sigaction);
	
	sigemptyset(&sa.sa_mask);
	sa.sa_restorer = NULL;
	sa.sa_handler = (__sighandler_t)A(arg);
	sa.sa_flags = 0;
	if (one_shot) sa.sa_flags = SA_ONESHOT | SA_NOMASK;
	set_fs (KERNEL_DS);
	ret = sys_sigaction(sig, &sa, &old);
	set_fs (old_fs);
	if (ret < 0) return ret;
	return (u32)(long)old.sa_handler;
}

static inline long solaris_signal(int sig, u32 arg)
{
	return sig_handler (sig, arg, 1);
}

static long solaris_sigset(int sig, u32 arg)
{
	if (arg != 2) /* HOLD */ {
		spin_lock_irq(&current->sigmask_lock);
		sigdelsetmask(&current->blocked, _S(sig));
		recalc_sigpending(current);
		spin_unlock_irq(&current->sigmask_lock);
		return sig_handler (sig, arg, 0);
	} else {
		spin_lock_irq(&current->sigmask_lock);
		sigaddsetmask(&current->blocked, (_S(sig) & ~_BLOCKABLE));
		recalc_sigpending(current);
		spin_unlock_irq(&current->sigmask_lock);
		return 0;
	}
}

static inline long solaris_sighold(int sig)
{
	return solaris_sigset(sig, 2);
}

static inline long solaris_sigrelse(int sig)
{
	spin_lock_irq(&current->sigmask_lock);
	sigdelsetmask(&current->blocked, _S(sig));
	recalc_sigpending(current);
	spin_unlock_irq(&current->sigmask_lock);
	return 0;
}

static inline long solaris_sigignore(int sig)
{
	return sig_handler (sig, (u32)SIG_IGN, 0);
}

static inline long solaris_sigpause(int sig)
{
	printk ("Need to support solaris sigpause\n");
	return -ENOSYS;
}

asmlinkage long solaris_sigfunc(int sig, u32 arg)
{
	int func = sig & ~0xff;
	
	sig = mapsig(sig & 0xff); 
	if (sig < 0) return sig; 
	switch (func) {
	case 0: return solaris_signal(sig, arg); 
	case 0x100: return solaris_sigset(sig, arg); 
	case 0x200: return solaris_sighold(sig);
	case 0x400: return solaris_sigrelse(sig); 
	case 0x800: return solaris_sigignore(sig); 
	case 0x1000: return solaris_sigpause(sig);
	}
	return -EINVAL;
}

typedef struct {
	u32 __sigbits[4];
} sol_sigset_t;

static inline int mapin(u32 *p, sigset_t *q)
{
	int i;
	u32 x;
	int sig;
	
	sigemptyset(q);
	x = p[0];
	for (i = 1; i <= SOLARIS_NSIGNALS; i++) {
		if (x & 1) {
			sig = solaris_to_linux_signals[i];
			if (sig == -1)
				return -EINVAL;
			sigaddsetmask(q, (1L << (sig - 1)));
		}
		x >>= 1;
		if (i == 32)
			x = p[1];
	}
	return 0;
}

static inline int mapout(sigset_t *q, u32 *p)
{
	int i;
	int sig;
	
	p[0] = 0;
	p[1] = 0;
	for (i = 1; i <= 32; i++) {
		if (sigismember(q, sigmask(i))) {
			sig = linux_to_solaris_signals[i];
			if (sig == -1)
				return -EINVAL;
			if (sig > 32)
				p[1] |= 1L << (sig - 33);
			else
				p[0] |= 1L << (sig - 1);
		}
	}
	return 0;
}

asmlinkage int solaris_sigprocmask(int how, u32 in, u32 out)
{
	sigset_t in_s, *ins, out_s, *outs;
	mm_segment_t old_fs = get_fs();
	int ret;
	int (*sys_sigprocmask)(int,sigset_t *,sigset_t *) = 
		(int (*)(int,sigset_t *,sigset_t *))SYS(sigprocmask);
	
	ins = NULL; outs = NULL;
	if (in) {
		u32 tmp[2];
		
		if (copy_from_user (tmp, (sol_sigset_t *)A(in), 2*sizeof(u32)))
			return -EFAULT;
		ins = &in_s;
		if (mapin (tmp, ins)) return -EINVAL;
	}
	if (out) outs = &out_s;
	set_fs (KERNEL_DS);
	ret = sys_sigprocmask((how == 3) ? SIG_SETMASK : how, ins, outs);
	set_fs (old_fs);
	if (ret) return ret;
	if (out) {
		u32 tmp[4];
		
		tmp[2] = 0; tmp[3] = 0;
		if (mapout (outs, tmp)) return -EINVAL;
		if (copy_to_user((sol_sigset_t *)A(out), tmp, 4*sizeof(u32)))
			return -EFAULT;
	}
	return 0;
}

asmlinkage long do_sol_sigsuspend(u32 mask)
{
	sigset_t s;
	u32 tmp[2];
		
	if (copy_from_user (tmp, (sol_sigset_t *)A(mask), 2*sizeof(u32)))
		return -EFAULT;
	if (mapin (tmp, &s)) return -EINVAL;
	return (long)s.sig[0];
}

struct sol_sigaction {
	int	sa_flags;
	u32	sa_handler;
	u32	sa_mask[4];
	int	sa_resv[2];
};

asmlinkage int solaris_sigaction(int sig, u32 act, u32 old)
{
	u32 tmp, tmp2[4];
	struct sigaction s, s2;
	int ret;
	mm_segment_t old_fs = get_fs();
	int (*sys_sigaction)(int,struct sigaction *,struct sigaction *) = 
		(int (*)(int,struct sigaction *,struct sigaction *))SYS(sigaction);
	
	sig = mapsig(sig); 
	if (sig < 0) {
		/* We cheat a little bit for Solaris only signals */
		if (old && clear_user((struct sol_sigaction *)A(old), sizeof(struct sol_sigaction)))
			return -EFAULT;
		return 0;
	}
	if (act) {
		if (get_user (tmp, &((struct sol_sigaction *)A(act))->sa_flags))
			return -EFAULT;
		s.sa_flags = 0;
		if (tmp & SOLARIS_SA_ONSTACK) s.sa_flags |= SA_STACK;
		if (tmp & SOLARIS_SA_RESTART) s.sa_flags |= SA_RESTART;
		if (tmp & SOLARIS_SA_NODEFER) s.sa_flags |= SA_NOMASK;
		if (tmp & SOLARIS_SA_RESETHAND) s.sa_flags |= SA_ONESHOT;
		if (tmp & SOLARIS_SA_NOCLDSTOP) s.sa_flags |= SA_NOCLDSTOP;
		if (get_user (tmp, &((struct sol_sigaction *)A(act))->sa_handler) ||
		    copy_from_user (tmp2, &((struct sol_sigaction *)A(act))->sa_mask, 2*sizeof(u32)))
			return -EFAULT;
		s.sa_handler = (__sighandler_t)A(tmp);
		if (mapin (tmp2, &s.sa_mask)) return -EINVAL;
		s.sa_restorer = 0;
	}
	set_fs(KERNEL_DS);
	ret = sys_sigaction(sig, act ? &s : NULL, old ? &s2 : NULL);
	set_fs(old_fs);
	if (ret) return ret;
	if (old) {
		if (mapout (&s2.sa_mask, tmp2)) return -EINVAL;
		tmp = 0; tmp2[2] = 0; tmp2[3] = 0;
		if (s2.sa_flags & SA_STACK) tmp |= SOLARIS_SA_ONSTACK;
		if (s2.sa_flags & SA_RESTART) tmp |= SOLARIS_SA_RESTART;
		if (s2.sa_flags & SA_NOMASK) tmp |= SOLARIS_SA_NODEFER;
		if (s2.sa_flags & SA_ONESHOT) tmp |= SOLARIS_SA_RESETHAND;
		if (s2.sa_flags & SA_NOCLDSTOP) tmp |= SOLARIS_SA_NOCLDSTOP;
		if (put_user (tmp, &((struct sol_sigaction *)A(old))->sa_flags) ||
		    __put_user ((u32)(long)s2.sa_handler, &((struct sol_sigaction *)A(old))->sa_handler) ||
		    copy_to_user (&((struct sol_sigaction *)A(old))->sa_mask, tmp2, 4*sizeof(u32)))
			return -EFAULT;
	}
	return 0;
}

asmlinkage int solaris_sigpending(int which, u32 set)
{
	sigset_t s;
	u32 tmp[4];
	switch (which) {
	case 1: /* sigpending */
		spin_lock_irq(&current->sigmask_lock);
		sigandsets(&s, &current->blocked, &current->signal);
		recalc_sigpending(current);
		spin_unlock_irq(&current->sigmask_lock);
		break;
	case 2: /* sigfillset - I just set signals which have linux equivalents */
		sigfillset(&s);
		break;
	default: return -EINVAL;
	}
	if (mapout (&s, tmp)) return -EINVAL;
	tmp[2] = 0; tmp[3] = 0;
	if (copy_to_user ((u32 *)A(set), tmp, sizeof(tmp)))
		return -EFAULT;
	return 0;
}

asmlinkage int solaris_wait(u32 stat_loc)
{
	int (*sys_wait4)(pid_t,unsigned int *, int, struct rusage *) =
		(int (*)(pid_t,unsigned int *, int, struct rusage *))SYS(wait4);
	int ret, status;
	
	ret = sys_wait4(-1, (unsigned int *)A(stat_loc), WUNTRACED, NULL);
	if (ret >= 0 && stat_loc) {
		if (get_user (status, (unsigned int *)A(stat_loc)))
			return -EFAULT;
		if (((status - 1) & 0xffff) < 0xff)
			status = linux_to_solaris_signals[status & 0x7f] & 0x7f;
		else if ((status & 0xff) == 0x7f)
			status = (linux_to_solaris_signals[(status >> 8) & 0xff] << 8) | 0x7f;
		if (__put_user (status, (unsigned int *)A(stat_loc)))
			return -EFAULT;
	}
	return ret;
}

asmlinkage int solaris_waitid(int idtype, s32 pid, u32 info, int options)
{
	int (*sys_wait4)(pid_t,unsigned int *, int, struct rusage *) =
		(int (*)(pid_t,unsigned int *, int, struct rusage *))SYS(wait4);
	int opts, status, ret;
	
	switch (idtype) {
	case 0: /* P_PID */ break;
	case 1: /* P_PGID */ pid = -pid; break;
	case 7: /* P_ALL */ pid = -1; break;
	default: return -EINVAL;
	}
	opts = 0;
	if (options & SOLARIS_WUNTRACED) opts |= WUNTRACED;
	if (options & SOLARIS_WNOHANG) opts |= WNOHANG;
	current->state = TASK_RUNNING;
	ret = sys_wait4(pid, (unsigned int *)A(info), opts, NULL);
	if (ret < 0) return ret;
	if (info) {
		struct sol_siginfo *s = (struct sol_siginfo *)A(info);
	
		if (get_user (status, (unsigned int *)A(info))) return -EFAULT;
		__put_user_ret (SOLARIS_SIGCLD, &s->si_signo, -EFAULT);
		__put_user_ret (ret, &s->_data._proc._pid, -EFAULT);
		switch (status & 0xff) {
		case 0: ret = SOLARIS_CLD_EXITED;
			status = (status >> 8) & 0xff;
			break;
		case 0x7f:
			status = (status >> 8) & 0xff;
			switch (status) {
			case SIGSTOP:
			case SIGTSTP: ret = SOLARIS_CLD_STOPPED;
			default: ret = SOLARIS_CLD_EXITED;
			}
			status = linux_to_solaris_signals[status];
			break;
		default:
			if (status & 0x80) ret = SOLARIS_CLD_DUMPED;
			else ret = SOLARIS_CLD_KILLED;
			status = linux_to_solaris_signals[status & 0x7f];
			break;
		}
		__put_user_ret (ret, &s->si_code, -EFAULT);
		__put_user_ret (status, &s->_data._proc._pdata._cld._status, -EFAULT);
	}
	return 0;
}

extern int svr4_setcontext(svr4_ucontext_t *c, struct pt_regs *regs);
extern int svr4_getcontext(svr4_ucontext_t *c, struct pt_regs *regs);

asmlinkage int solaris_context(struct pt_regs *regs)
{
	switch ((unsigned)regs->u_regs[UREG_I0]) {
	case 0: /* getcontext */
		return svr4_getcontext((svr4_ucontext_t *)(long)(u32)regs->u_regs[UREG_I1], regs);
	case 1: /* setcontext */
		return svr4_setcontext((svr4_ucontext_t *)(long)(u32)regs->u_regs[UREG_I1], regs);
	default:
		return -EINVAL;

	}
}

asmlinkage int solaris_sigaltstack(u32 ss, u32 oss)
{
/* XXX Implement this soon */
	return 0;
}