2002-07-09 16:24:59 +04:00
|
|
|
/*
|
2004-12-01 07:26:10 +03:00
|
|
|
* Copyright 2004, Haiku Inc. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-12-01 07:26:10 +03:00
|
|
|
/* Big case statement for dispatching syscalls */
|
2003-10-28 16:29:29 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <kernel.h>
|
|
|
|
#include <ksyscalls.h>
|
2002-10-23 21:31:10 +04:00
|
|
|
#include <syscalls.h>
|
2004-12-14 18:49:20 +03:00
|
|
|
#include <generic_syscall.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <int.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <vfs.h>
|
2004-12-14 02:02:18 +03:00
|
|
|
#include <vm.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <thread.h>
|
|
|
|
#include <sem.h>
|
|
|
|
#include <port.h>
|
|
|
|
#include <cpu.h>
|
2004-09-10 19:20:37 +04:00
|
|
|
#include <arch_config.h>
|
2004-10-29 05:37:37 +04:00
|
|
|
#include <disk_device_manager/ddm_userland_interface.h>
|
2003-09-21 00:47:27 +04:00
|
|
|
#include <sys/resource.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <fd.h>
|
2003-01-18 17:18:04 +03:00
|
|
|
#include <fs/node_monitor.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <sysctl.h>
|
|
|
|
#include <ksocket.h>
|
2003-01-12 19:27:03 +03:00
|
|
|
#include <kimage.h>
|
2003-01-27 06:07:30 +03:00
|
|
|
#include <ksignal.h>
|
2003-10-28 16:29:29 +03:00
|
|
|
#include <real_time_clock.h>
|
2004-04-22 02:57:39 +04:00
|
|
|
#include <system_info.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <sys/ioccom.h>
|
2002-07-12 02:21:56 +04:00
|
|
|
#include <sys/socket.h>
|
2003-08-22 03:02:00 +04:00
|
|
|
#include <user_atomic.h>
|
2004-11-27 15:35:30 +03:00
|
|
|
#include <safemode.h>
|
2004-12-01 07:26:10 +03:00
|
|
|
#include <arch/system_info.h>
|
2004-12-14 20:09:06 +03:00
|
|
|
|
2004-12-14 18:49:20 +03:00
|
|
|
#include <malloc.h>
|
2004-12-14 20:09:06 +03:00
|
|
|
#include <string.h>
|
2003-08-20 06:34:42 +04:00
|
|
|
|
2003-10-28 16:29:29 +03:00
|
|
|
|
2004-12-14 18:49:20 +03:00
|
|
|
typedef struct generic_syscall generic_syscall;
|
|
|
|
|
|
|
|
struct generic_syscall {
|
|
|
|
list_link link;
|
2004-12-14 20:09:06 +03:00
|
|
|
char subsystem[B_FILE_NAME_LENGTH];
|
2004-12-14 18:49:20 +03:00
|
|
|
syscall_hook hook;
|
|
|
|
uint32 version;
|
|
|
|
uint32 flags;
|
|
|
|
generic_syscall *previous;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct mutex sGenericSyscallLock;
|
|
|
|
static struct list sGenericSyscalls;
|
|
|
|
|
|
|
|
|
|
|
|
static generic_syscall *
|
2004-12-14 20:09:06 +03:00
|
|
|
find_generic_syscall(const char *subsystem)
|
2004-12-14 18:49:20 +03:00
|
|
|
{
|
|
|
|
generic_syscall *syscall = NULL;
|
|
|
|
|
|
|
|
ASSERT_LOCKED_MUTEX(&sGenericSyscallLock);
|
|
|
|
|
|
|
|
while ((syscall = list_get_next_item(&sGenericSyscalls, syscall)) != NULL) {
|
2004-12-14 20:09:06 +03:00
|
|
|
if (!strcmp(syscall->subsystem, subsystem))
|
2004-12-14 18:49:20 +03:00
|
|
|
return syscall;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Calls the generic syscall subsystem if any.
|
|
|
|
* Also handles the special generic syscall function \c B_SYSCALL_INFO.
|
|
|
|
* Returns \c B_NAME_NOT_FOUND if either the subsystem was not found, or
|
|
|
|
* the subsystem does not support the requested function.
|
|
|
|
* All other return codes are depending on the generic syscall implementation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline status_t
|
2004-12-14 20:09:06 +03:00
|
|
|
_user_generic_syscall(const char *userSubsystem, uint32 function,
|
|
|
|
void *buffer, size_t bufferSize)
|
2004-12-14 18:49:20 +03:00
|
|
|
{
|
2004-12-14 20:09:06 +03:00
|
|
|
char subsystem[B_FILE_NAME_LENGTH];
|
2004-12-14 18:49:20 +03:00
|
|
|
generic_syscall *syscall;
|
2005-01-20 20:58:58 +03:00
|
|
|
status_t status = B_NAME_NOT_FOUND;
|
2004-12-14 18:49:20 +03:00
|
|
|
|
2004-12-14 20:09:06 +03:00
|
|
|
if (!IS_USER_ADDRESS(userSubsystem)
|
|
|
|
|| user_strlcpy(subsystem, userSubsystem, sizeof(subsystem)) < B_OK)
|
|
|
|
return B_BAD_ADDRESS;
|
|
|
|
|
|
|
|
//dprintf("generic_syscall(subsystem = \"%s\", function = %lu)\n", subsystem, function);
|
2004-12-14 18:49:20 +03:00
|
|
|
|
|
|
|
mutex_lock(&sGenericSyscallLock);
|
|
|
|
|
|
|
|
syscall = find_generic_syscall(subsystem);
|
2005-01-20 20:58:58 +03:00
|
|
|
if (syscall == NULL)
|
2004-12-14 18:49:20 +03:00
|
|
|
goto out;
|
|
|
|
|
2004-12-14 20:09:06 +03:00
|
|
|
if (function >= B_RESERVED_SYSCALL_BASE) {
|
|
|
|
if (function != B_SYSCALL_INFO) {
|
|
|
|
// this is all we know
|
|
|
|
status = B_NAME_NOT_FOUND;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2004-12-14 18:49:20 +03:00
|
|
|
// special info syscall
|
|
|
|
if (bufferSize != sizeof(uint32))
|
|
|
|
status = B_BAD_VALUE;
|
|
|
|
else {
|
|
|
|
uint32 requestedVersion;
|
|
|
|
|
|
|
|
// retrieve old version
|
|
|
|
status = user_memcpy(&requestedVersion, buffer, sizeof(uint32));
|
|
|
|
if (status == B_OK && requestedVersion != 0 && requestedVersion < syscall->version)
|
|
|
|
status = B_BAD_TYPE;
|
|
|
|
|
|
|
|
// return current version
|
|
|
|
if (status == B_OK)
|
|
|
|
status = user_memcpy(buffer, &syscall->version, sizeof(uint32));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (syscall != NULL) {
|
|
|
|
generic_syscall *next;
|
|
|
|
|
|
|
|
mutex_unlock(&sGenericSyscallLock);
|
|
|
|
|
|
|
|
status = syscall->hook(subsystem, function, buffer, bufferSize);
|
|
|
|
|
|
|
|
mutex_lock(&sGenericSyscallLock);
|
|
|
|
if (status != B_BAD_HANDLER)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// the syscall may have been removed in the mean time
|
|
|
|
next = find_generic_syscall(subsystem);
|
|
|
|
if (next == syscall)
|
|
|
|
syscall = syscall->previous;
|
|
|
|
else
|
|
|
|
syscall = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (syscall == NULL)
|
|
|
|
status = B_NAME_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&sGenericSyscallLock);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline int
|
2004-08-29 00:34:43 +04:00
|
|
|
_user_null()
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-08-29 00:34:43 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2003-08-20 06:34:42 +04:00
|
|
|
|
2004-12-01 07:26:10 +03:00
|
|
|
|
2004-10-01 03:25:55 +04:00
|
|
|
// map to the arch specific call
|
2004-12-14 18:49:20 +03:00
|
|
|
|
|
|
|
static inline int64
|
2004-08-29 00:34:43 +04:00
|
|
|
_user_restore_signal_frame()
|
|
|
|
{
|
|
|
|
return arch_restore_signal_frame();
|
|
|
|
}
|
2003-01-18 17:18:04 +03:00
|
|
|
|
2003-10-28 16:29:29 +03:00
|
|
|
|
2004-08-29 00:34:43 +04:00
|
|
|
// TODO: Replace when networking code is added to the build.
|
2004-12-14 18:49:20 +03:00
|
|
|
|
|
|
|
static inline int
|
2004-08-29 00:34:43 +04:00
|
|
|
_user_socket(int family, int type, int proto)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2003-10-28 16:29:29 +03:00
|
|
|
|
2003-11-14 01:08:30 +03:00
|
|
|
|
2004-12-14 18:49:20 +03:00
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
int32
|
|
|
|
syscall_dispatcher(uint32 call_num, void *args, uint64 *call_ret)
|
2004-08-29 00:34:43 +04:00
|
|
|
{
|
|
|
|
// dprintf("syscall_dispatcher: thread 0x%x call 0x%x, arg0 0x%x, arg1 0x%x arg2 0x%x arg3 0x%x arg4 0x%x\n",
|
|
|
|
// thread_get_current_thread_id(), call_num, arg0, arg1, arg2, arg3, arg4);
|
2003-08-22 03:02:00 +04:00
|
|
|
|
2004-08-29 00:34:43 +04:00
|
|
|
switch (call_num) {
|
|
|
|
// the cases are auto-generated
|
|
|
|
#include "syscall_dispatcher.h"
|
2003-08-22 03:02:00 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
default:
|
|
|
|
*call_ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dprintf("syscall_dispatcher: done with syscall 0x%x\n", call_num);
|
|
|
|
|
2002-07-19 20:07:36 +04:00
|
|
|
return B_INVOKE_SCHEDULER;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2004-12-14 18:49:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
generic_syscall_init(void)
|
|
|
|
{
|
|
|
|
list_init(&sGenericSyscalls);
|
|
|
|
return mutex_init(&sGenericSyscallLock, "generic syscall");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
// public API
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2004-12-14 20:09:06 +03:00
|
|
|
register_generic_syscall(const char *subsystem, syscall_hook hook,
|
2004-12-14 18:49:20 +03:00
|
|
|
uint32 version, uint32 flags)
|
|
|
|
{
|
|
|
|
struct generic_syscall *previous, *syscall;
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
if (hook == NULL)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
mutex_lock(&sGenericSyscallLock);
|
|
|
|
|
|
|
|
previous = find_generic_syscall(subsystem);
|
|
|
|
if (previous != NULL) {
|
|
|
|
if ((flags & B_DO_NOT_REPLACE_SYSCALL) != 0
|
|
|
|
|| version < previous->version) {
|
|
|
|
status = B_NAME_IN_USE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (previous->flags & B_SYSCALL_NOT_REPLACEABLE) {
|
|
|
|
status = B_NOT_ALLOWED;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
syscall = (generic_syscall *)malloc(sizeof(struct generic_syscall));
|
|
|
|
if (syscall == NULL) {
|
|
|
|
status = B_NO_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2004-12-14 20:09:06 +03:00
|
|
|
strlcpy(syscall->subsystem, subsystem, sizeof(syscall->subsystem));
|
2004-12-14 18:49:20 +03:00
|
|
|
syscall->hook = hook;
|
|
|
|
syscall->version = version;
|
|
|
|
syscall->flags = flags;
|
|
|
|
syscall->previous = previous;
|
|
|
|
list_add_item(&sGenericSyscalls, syscall);
|
|
|
|
|
|
|
|
if (previous != NULL)
|
|
|
|
list_remove_link(&previous->link);
|
|
|
|
|
|
|
|
status = B_OK;
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&sGenericSyscallLock);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2004-12-14 20:09:06 +03:00
|
|
|
unregister_generic_syscall(const char *subsystem, uint32 version)
|
2004-12-14 18:49:20 +03:00
|
|
|
{
|
|
|
|
// ToDo: we should only remove the syscall with the matching version
|
|
|
|
generic_syscall *syscall;
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
mutex_lock(&sGenericSyscallLock);
|
|
|
|
|
|
|
|
syscall = find_generic_syscall(subsystem);
|
|
|
|
if (syscall != NULL) {
|
|
|
|
if (syscall->previous != NULL) {
|
|
|
|
// reestablish the old syscall
|
|
|
|
list_add_item(&sGenericSyscalls, syscall->previous);
|
|
|
|
}
|
|
|
|
list_remove_link(&syscall->link);
|
|
|
|
free(syscall);
|
|
|
|
status = B_OK;
|
|
|
|
} else
|
|
|
|
status = B_NAME_NOT_FOUND;
|
|
|
|
|
|
|
|
mutex_unlock(&sGenericSyscallLock);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|