|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache 2.0 |
| 5 | + */ |
| 6 | + |
| 7 | + |
| 8 | +#ifndef _ZEPHYR_SYSCALL_H_ |
| 9 | +#define _ZEPHYR_SYSCALL_H_ |
| 10 | + |
| 11 | +/* Fixed system call IDs. We use #defines instead of enumeration so that if |
| 12 | + * system calls are retired it does not shift the IDs of other system calls. |
| 13 | + */ |
| 14 | +#define K_SYSCALL_BAD 0 |
| 15 | + |
| 16 | +#define K_SYSCALL_LIMIT 1 |
| 17 | + |
| 18 | +#ifndef _ASMLANGUAGE |
| 19 | +#include <misc/printk.h> |
| 20 | + |
| 21 | +/** |
| 22 | + * @typedef _k_syscall_handler_t |
| 23 | + * @brief System call handler function type |
| 24 | + * |
| 25 | + * These are kernel-side skeleton functions for system calls. They are |
| 26 | + * necessary to sanitize the arguments passed into the system call: |
| 27 | + * |
| 28 | + * - Any kernel object or device pointers are validated with _SYSCALL_IS_OBJ() |
| 29 | + * - Any memory buffers passed in are checked to ensure that the calling thread |
| 30 | + * actually has access to them |
| 31 | + * - Many kernel calls do no sanity checking of parameters other than |
| 32 | + * assertions. The handler must check all of these conditions using |
| 33 | + * _SYSCALL_ASSERT() |
| 34 | + * - If the system call has more then 5 arguments, then arg5 will be a pointer |
| 35 | + * to some struct containing arguments 5+. The struct itself needs to be |
| 36 | + * validated like any other buffer passed in from userspace, and its members |
| 37 | + * individually validated (if necessary) and then passed to the real |
| 38 | + * implementation like normal arguments |
| 39 | + * |
| 40 | + * Even if the system call implementation has no return value, these always |
| 41 | + * return something, even 0, to prevent register leakage to userspace. |
| 42 | + * |
| 43 | + * Once everything has been validated, the real implementation will be executed. |
| 44 | + * |
| 45 | + * @param arg1 system call argument 1 |
| 46 | + * @param arg2 system call argument 2 |
| 47 | + * @param arg3 system call argument 3 |
| 48 | + * @param arg4 system call argument 4 |
| 49 | + * @param arg5 system call argument 5 |
| 50 | + * @param ssf System call stack frame pointer. Used to generate kernel oops |
| 51 | + * via _arch_syscall_oops_at(). Contents are arch-specific. |
| 52 | + * @return system call return value, or 0 if the system call implementation |
| 53 | + * return void |
| 54 | + * |
| 55 | + */ |
| 56 | +typedef u32_t (*_k_syscall_handler_t)(u32_t arg1, u32_t arg2, u32_t arg3, |
| 57 | + u32_t arg4, u32_t arg5, void *ssf); |
| 58 | + |
| 59 | + |
| 60 | +extern const _k_syscall_handler_t _k_syscall_table[K_SYSCALL_LIMIT]; |
| 61 | + |
| 62 | + |
| 63 | +/** |
| 64 | + * @brief Runtime expression check for system call arguments |
| 65 | + * |
| 66 | + * Used in handler functions to perform various runtime checks on arguments, |
| 67 | + * and generate a kernel oops if anything is not expected |
| 68 | + * |
| 69 | + * @param expr Boolean expression to verify, a false result will trigger an |
| 70 | + * oops |
| 71 | + * @param ssf Syscall stack frame argument passed to the handler function |
| 72 | + */ |
| 73 | +#define _SYSCALL_VERIFY(expr, ssf) \ |
| 74 | + do { \ |
| 75 | + if (!(expr)) { \ |
| 76 | + printk("FATAL: syscall failed check: " #expr "\n"); \ |
| 77 | + _arch_syscall_oops(ssf); \ |
| 78 | + } \ |
| 79 | + } while (0) |
| 80 | + |
| 81 | +/** |
| 82 | + * @brief Runtime check that a pointer is a kernel object of expected type |
| 83 | + * |
| 84 | + * Passes along arguments to _k_object_validate() and triggers a kernel oops |
| 85 | + * if the object wasn't valid or had incorrect permissions. |
| 86 | + * |
| 87 | + * @param ptr Untrusted kernel object pointer |
| 88 | + * @param type Expected kernel object type |
| 89 | + * @param init Whether this is an init function handler |
| 90 | + * @param ssf Syscall stack frame argument passed to the handler function |
| 91 | + */ |
| 92 | +#define _SYSCALL_IS_OBJ(ptr, type, init, ssf) \ |
| 93 | + _SYSCALL_VERIFY(!_k_object_validate((void *)ptr, type, init), ssf) |
| 94 | + |
| 95 | +/* Convenience macros for handler implementations */ |
| 96 | +#define _SYSCALL_ARG0 ARG_UNUSED(arg1); ARG_UNUSED(arg2); ARG_UNUSED(arg3); \ |
| 97 | + ARG_UNUSED(arg4); ARH_UNUSED(arg5) |
| 98 | + |
| 99 | +#define _SYSCALL_ARG1 ARG_UNUSED(arg2); ARG_UNUSED(arg3); ARG_UNUSED(arg4); \ |
| 100 | + ARG_UNUSED(arg5) |
| 101 | + |
| 102 | +#define _SYSCALL_ARG2 ARG_UNUSED(arg3); ARG_UNUSED(arg4); ARG_UNUSED(arg5) |
| 103 | + |
| 104 | +#define _SYSCALL_ARG3 ARG_UNUSED(arg4); ARG_UNUSED(arg5) |
| 105 | + |
| 106 | +#define _SYSCALL_ARG4 ARG_UNUSED(arg5) |
| 107 | + |
| 108 | + |
| 109 | +#endif /* _ASMLANGUAGE */ |
| 110 | + |
| 111 | +#endif /* _ZEPHYR_SYSCALL_H_ */ |
0 commit comments