kern_syscalls.c revision 92547
1254721Semaste/*-
2254721Semaste * Copyright (c) 1999 Assar Westerlund
3254721Semaste * All rights reserved.
4254721Semaste *
5254721Semaste * Redistribution and use in source and binary forms, with or without
6254721Semaste * modification, are permitted provided that the following conditions
7254721Semaste * are met:
8254721Semaste * 1. Redistributions of source code must retain the above copyright
9254721Semaste *    notice, this list of conditions and the following disclaimer.
10254721Semaste * 2. Redistributions in binary form must reproduce the above copyright
11254721Semaste *    notice, this list of conditions and the following disclaimer in the
12254721Semaste *    documentation and/or other materials provided with the distribution.
13254721Semaste *
14254721Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16254721Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17254721Semaste * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24254721Semaste * SUCH DAMAGE.
25254721Semaste *
26254721Semaste * $FreeBSD: head/sys/kern/kern_syscalls.c 92547 2002-03-18 07:45:30Z arr $
27254721Semaste */
28254721Semaste
29254721Semaste#include <sys/param.h>
30254721Semaste#include <sys/sysproto.h>
31254721Semaste#include <sys/sysent.h>
32254721Semaste#include <sys/syscall.h>
33254721Semaste#include <sys/lock.h>
34254721Semaste#include <sys/mutex.h>
35254721Semaste#include <sys/sx.h>
36254721Semaste#include <sys/module.h>
37254721Semaste
38254721Semaste/*
39254721Semaste * Acts like "nosys" but can be identified in sysent for dynamic call
40254721Semaste * number assignment for a limited number of calls.
41254721Semaste *
42254721Semaste * Place holder for system call slots reserved for loadable modules.
43254721Semaste */
44254721Semasteint
45254721Semastelkmnosys(struct thread *td, struct nosys_args *args)
46254721Semaste{
47254721Semaste	return(nosys(td, args));
48254721Semaste}
49254721Semaste
50254721Semasteint
51254721Semastelkmressys(struct thread *td, struct nosys_args *args)
52254721Semaste{
53254721Semaste	return(nosys(td, args));
54254721Semaste}
55254721Semaste
56254721Semasteint
57254721Semastesyscall_register(int *offset, struct sysent *new_sysent,
58254721Semaste		 struct sysent *old_sysent)
59254721Semaste{
60254721Semaste       if (*offset == NO_SYSCALL) {
61254721Semaste               int i;
62254721Semaste
63254721Semaste               for (i = 1; i < SYS_MAXSYSCALL; ++i)
64254721Semaste                       if (sysent[i].sy_call == (sy_call_t *)lkmnosys)
65254721Semaste                               break;
66254721Semaste               if (i == SYS_MAXSYSCALL)
67254721Semaste                       return ENFILE;
68254721Semaste               *offset = i;
69254721Semaste       } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
70254721Semaste               return EINVAL;
71254721Semaste       else if (sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
72254721Semaste				sysent[*offset].sy_call != (sy_call_t *)lkmressys)
73254721Semaste               return EEXIST;
74254721Semaste
75254721Semaste       *old_sysent = sysent[*offset];
76254721Semaste       sysent[*offset] = *new_sysent;
77254721Semaste       return 0;
78254721Semaste}
79254721Semaste
80254721Semasteint
81254721Semastesyscall_deregister(int *offset, struct sysent *old_sysent)
82254721Semaste{
83254721Semaste       if (*offset)
84254721Semaste               sysent[*offset] = *old_sysent;
85254721Semaste       return 0;
86254721Semaste}
87254721Semaste
88254721Semasteint
89254721Semastesyscall_module_handler(struct module *mod, int what, void *arg)
90254721Semaste{
91254721Semaste       struct syscall_module_data *data = (struct syscall_module_data*)arg;
92254721Semaste       modspecific_t ms;
93254721Semaste       int error;
94254721Semaste
95254721Semaste       switch (what) {
96254721Semaste       case MOD_LOAD :
97254721Semaste               error = syscall_register(data->offset, data->new_sysent,
98254721Semaste                                        &data->old_sysent);
99254721Semaste               if (error)
100254721Semaste                       return error;
101254721Semaste	       ms.intval = *data->offset;
102254721Semaste	       MOD_XLOCK;
103254721Semaste	       module_setspecific(mod, &ms);
104254721Semaste	       MOD_XUNLOCK;
105254721Semaste               if (data->chainevh)
106254721Semaste                       error = data->chainevh(mod, what, data->chainarg);
107254721Semaste               return error;
108254721Semaste
109254721Semaste       case MOD_UNLOAD :
110254721Semaste               if (data->chainevh) {
111254721Semaste                       error = data->chainevh(mod, what, data->chainarg);
112254721Semaste                       if (error)
113254721Semaste                               return error;
114254721Semaste               }
115254721Semaste               error = syscall_deregister(data->offset, &data->old_sysent);
116254721Semaste               return error;
117254721Semaste       }
118254721Semaste
119254721Semaste       if (data->chainevh)
120254721Semaste               return data->chainevh(mod, what, data->chainarg);
121254721Semaste       else
122254721Semaste               return 0;
123254721Semaste}
124254721Semaste