Andrew Boie | a23c245 | 2017-09-13 18:04:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, Intel Corporation |
| 3 | * |
Anas Nashif | c7f5cc9 | 2018-04-12 13:45:33 -0500 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Andrew Boie | a23c245 | 2017-09-13 18:04:21 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 8 | #ifndef ZEPHYR_INCLUDE_SYSCALL_H_ |
| 9 | #define ZEPHYR_INCLUDE_SYSCALL_H_ |
Andrew Boie | a23c245 | 2017-09-13 18:04:21 -0700 | [diff] [blame] | 10 | |
Sebastian Bøe | 1186f5b | 2018-08-10 15:43:31 +0200 | [diff] [blame] | 11 | #include <syscall_list.h> |
| 12 | #include <arch/syscall.h> |
Flavio Ceolin | 0bf21ca | 2018-11-21 17:46:38 -0800 | [diff] [blame] | 13 | #include <stdbool.h> |
Sebastian Bøe | 1186f5b | 2018-08-10 15:43:31 +0200 | [diff] [blame] | 14 | |
Andrew Boie | a23c245 | 2017-09-13 18:04:21 -0700 | [diff] [blame] | 15 | #ifndef _ASMLANGUAGE |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 16 | #include <zephyr/types.h> |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 17 | |
Andrew Boie | 0d9a9be | 2017-09-29 16:51:36 -0700 | [diff] [blame] | 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 22 | /* |
| 23 | * System Call Declaration macros |
| 24 | * |
| 25 | * These macros are used in public header files to declare system calls. |
| 26 | * They generate inline functions which have different implementations |
| 27 | * depending on the current compilation context: |
| 28 | * |
| 29 | * - Kernel-only code, or CONFIG_USERSPACE disabled, these inlines will |
| 30 | * directly call the implementation |
| 31 | * - User-only code, these inlines will marshal parameters and elevate |
| 32 | * privileges |
| 33 | * - Mixed or indeterminate code, these inlines will do a runtime check |
| 34 | * to determine what course of action is needed. |
| 35 | * |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 36 | * All system calls require a verifier function and an implementation |
| 37 | * function. These must follow a naming convention. For a system call |
| 38 | * named k_foo(): |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 39 | * |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 40 | * - The handler function will be named z_vrfy_k_foo(). Handler |
| 41 | * functions have the same type signature as the wrapped call, |
| 42 | * verify arguments passed up from userspace, and call the |
| 43 | * implementation function. See documentation for that typedef for |
| 44 | * more information. - The implementation function will be named |
| 45 | * z_impl_k_foo(). This is the actual implementation of the system |
| 46 | * call. |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 47 | */ |
Andrew Boie | a23c245 | 2017-09-13 18:04:21 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * @typedef _k_syscall_handler_t |
| 51 | * @brief System call handler function type |
| 52 | * |
| 53 | * These are kernel-side skeleton functions for system calls. They are |
| 54 | * necessary to sanitize the arguments passed into the system call: |
| 55 | * |
| 56 | * - Any kernel object or device pointers are validated with _SYSCALL_IS_OBJ() |
| 57 | * - Any memory buffers passed in are checked to ensure that the calling thread |
| 58 | * actually has access to them |
| 59 | * - Many kernel calls do no sanity checking of parameters other than |
| 60 | * assertions. The handler must check all of these conditions using |
| 61 | * _SYSCALL_ASSERT() |
Andrew Boie | 1956f09 | 2017-09-19 09:59:42 -0700 | [diff] [blame] | 62 | * - If the system call has more than 6 arguments, then arg6 will be a pointer |
| 63 | * to some struct containing arguments 6+. The struct itself needs to be |
Andrew Boie | a23c245 | 2017-09-13 18:04:21 -0700 | [diff] [blame] | 64 | * validated like any other buffer passed in from userspace, and its members |
| 65 | * individually validated (if necessary) and then passed to the real |
| 66 | * implementation like normal arguments |
| 67 | * |
| 68 | * Even if the system call implementation has no return value, these always |
| 69 | * return something, even 0, to prevent register leakage to userspace. |
| 70 | * |
| 71 | * Once everything has been validated, the real implementation will be executed. |
| 72 | * |
| 73 | * @param arg1 system call argument 1 |
| 74 | * @param arg2 system call argument 2 |
| 75 | * @param arg3 system call argument 3 |
| 76 | * @param arg4 system call argument 4 |
| 77 | * @param arg5 system call argument 5 |
Andrew Boie | 1956f09 | 2017-09-19 09:59:42 -0700 | [diff] [blame] | 78 | * @param arg6 system call argument 6 |
Andrew Boie | a23c245 | 2017-09-13 18:04:21 -0700 | [diff] [blame] | 79 | * @param ssf System call stack frame pointer. Used to generate kernel oops |
| 80 | * via _arch_syscall_oops_at(). Contents are arch-specific. |
| 81 | * @return system call return value, or 0 if the system call implementation |
| 82 | * return void |
| 83 | * |
| 84 | */ |
Andrew Boie | 800b35f | 2019-11-05 09:27:18 -0800 | [diff] [blame] | 85 | typedef uintptr_t (*_k_syscall_handler_t)(uintptr_t arg1, uintptr_t arg2, |
| 86 | uintptr_t arg3, uintptr_t arg4, |
| 87 | uintptr_t arg5, uintptr_t arg6, |
| 88 | void *ssf); |
Piotr Zięcik | 78eb718 | 2019-08-22 11:03:34 +0200 | [diff] [blame] | 89 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 90 | /* True if a syscall function must trap to the kernel, usually a |
| 91 | * compile-time decision. |
| 92 | */ |
| 93 | static ALWAYS_INLINE bool z_syscall_trap(void) |
| 94 | { |
Andy Ross | 643701a | 2019-08-13 12:58:38 -0700 | [diff] [blame] | 95 | bool ret = false; |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 96 | #ifdef CONFIG_USERSPACE |
| 97 | #if defined(__ZEPHYR_SUPERVISOR__) |
Andy Ross | 643701a | 2019-08-13 12:58:38 -0700 | [diff] [blame] | 98 | ret = false; |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 99 | #elif defined(__ZEPHYR_USER__) |
Andy Ross | 643701a | 2019-08-13 12:58:38 -0700 | [diff] [blame] | 100 | ret = true; |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 101 | #else |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 102 | ret = arch_is_user_context(); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 103 | #endif |
| 104 | #endif |
Andy Ross | 643701a | 2019-08-13 12:58:38 -0700 | [diff] [blame] | 105 | return ret; |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 106 | } |
Andrew Boie | 0d9a9be | 2017-09-29 16:51:36 -0700 | [diff] [blame] | 107 | |
Piotr Zięcik | 78eb718 | 2019-08-22 11:03:34 +0200 | [diff] [blame] | 108 | /** |
| 109 | * Indicate whether the CPU is currently in user mode |
| 110 | * |
| 111 | * @return true if the CPU is currently running with user permissions |
| 112 | */ |
Anas Nashif | 0ec3774 | 2021-03-27 12:03:18 -0400 | [diff] [blame] | 113 | static inline bool k_is_user_context(void) |
Piotr Zięcik | 78eb718 | 2019-08-22 11:03:34 +0200 | [diff] [blame] | 114 | { |
| 115 | #ifdef CONFIG_USERSPACE |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 116 | return arch_is_user_context(); |
Piotr Zięcik | 78eb718 | 2019-08-22 11:03:34 +0200 | [diff] [blame] | 117 | #else |
| 118 | return false; |
| 119 | #endif |
| 120 | } |
| 121 | |
Andrew Boie | 0d9a9be | 2017-09-29 16:51:36 -0700 | [diff] [blame] | 122 | #ifdef __cplusplus |
| 123 | } |
| 124 | #endif |
| 125 | |
Andrew Boie | a23c245 | 2017-09-13 18:04:21 -0700 | [diff] [blame] | 126 | #endif /* _ASMLANGUAGE */ |
| 127 | |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 128 | #endif |