blob: 871c24406730f8a6bb7fd77cf72604c3241b89ab [file] [log] [blame]
Andrew Boiea23c2452017-09-13 18:04:21 -07001/*
2 * Copyright (c) 2017, Intel Corporation
3 *
Anas Nashifc7f5cc92018-04-12 13:45:33 -05004 * SPDX-License-Identifier: Apache-2.0
Andrew Boiea23c2452017-09-13 18:04:21 -07005 */
6
7
Flavio Ceolin67ca1762018-09-14 10:43:44 -07008#ifndef ZEPHYR_INCLUDE_SYSCALL_H_
9#define ZEPHYR_INCLUDE_SYSCALL_H_
Andrew Boiea23c2452017-09-13 18:04:21 -070010
Sebastian Bøe1186f5b2018-08-10 15:43:31 +020011#include <syscall_list.h>
12#include <arch/syscall.h>
Flavio Ceolin0bf21ca2018-11-21 17:46:38 -080013#include <stdbool.h>
Sebastian Bøe1186f5b2018-08-10 15:43:31 +020014
Andrew Boiea23c2452017-09-13 18:04:21 -070015#ifndef _ASMLANGUAGE
Andrew Boie13ca6fe2017-09-23 12:05:49 -070016#include <zephyr/types.h>
Daniel Leung7ad00b92021-07-22 13:13:17 -070017#include <linker/sections.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070018
Andrew Boie0d9a9be2017-09-29 16:51:36 -070019#ifdef __cplusplus
20extern "C" {
21#endif
22
Andrew Boie13ca6fe2017-09-23 12:05:49 -070023/*
24 * System Call Declaration macros
25 *
26 * These macros are used in public header files to declare system calls.
27 * They generate inline functions which have different implementations
28 * depending on the current compilation context:
29 *
30 * - Kernel-only code, or CONFIG_USERSPACE disabled, these inlines will
31 * directly call the implementation
32 * - User-only code, these inlines will marshal parameters and elevate
33 * privileges
34 * - Mixed or indeterminate code, these inlines will do a runtime check
35 * to determine what course of action is needed.
36 *
Andy Ross65649742019-08-06 13:34:31 -070037 * All system calls require a verifier function and an implementation
38 * function. These must follow a naming convention. For a system call
39 * named k_foo():
Andrew Boie13ca6fe2017-09-23 12:05:49 -070040 *
Andy Ross65649742019-08-06 13:34:31 -070041 * - The handler function will be named z_vrfy_k_foo(). Handler
42 * functions have the same type signature as the wrapped call,
43 * verify arguments passed up from userspace, and call the
44 * implementation function. See documentation for that typedef for
45 * more information. - The implementation function will be named
46 * z_impl_k_foo(). This is the actual implementation of the system
47 * call.
Andrew Boie13ca6fe2017-09-23 12:05:49 -070048 */
Andrew Boiea23c2452017-09-13 18:04:21 -070049
50/**
51 * @typedef _k_syscall_handler_t
52 * @brief System call handler function type
53 *
54 * These are kernel-side skeleton functions for system calls. They are
55 * necessary to sanitize the arguments passed into the system call:
56 *
57 * - Any kernel object or device pointers are validated with _SYSCALL_IS_OBJ()
58 * - Any memory buffers passed in are checked to ensure that the calling thread
59 * actually has access to them
60 * - Many kernel calls do no sanity checking of parameters other than
61 * assertions. The handler must check all of these conditions using
62 * _SYSCALL_ASSERT()
Andrew Boie1956f092017-09-19 09:59:42 -070063 * - If the system call has more than 6 arguments, then arg6 will be a pointer
64 * to some struct containing arguments 6+. The struct itself needs to be
Andrew Boiea23c2452017-09-13 18:04:21 -070065 * validated like any other buffer passed in from userspace, and its members
66 * individually validated (if necessary) and then passed to the real
67 * implementation like normal arguments
68 *
69 * Even if the system call implementation has no return value, these always
70 * return something, even 0, to prevent register leakage to userspace.
71 *
72 * Once everything has been validated, the real implementation will be executed.
73 *
74 * @param arg1 system call argument 1
75 * @param arg2 system call argument 2
76 * @param arg3 system call argument 3
77 * @param arg4 system call argument 4
78 * @param arg5 system call argument 5
Andrew Boie1956f092017-09-19 09:59:42 -070079 * @param arg6 system call argument 6
Andrew Boiea23c2452017-09-13 18:04:21 -070080 * @param ssf System call stack frame pointer. Used to generate kernel oops
81 * via _arch_syscall_oops_at(). Contents are arch-specific.
82 * @return system call return value, or 0 if the system call implementation
83 * return void
84 *
85 */
Andrew Boie800b35f2019-11-05 09:27:18 -080086typedef uintptr_t (*_k_syscall_handler_t)(uintptr_t arg1, uintptr_t arg2,
87 uintptr_t arg3, uintptr_t arg4,
88 uintptr_t arg5, uintptr_t arg6,
89 void *ssf);
Piotr Zięcik78eb7182019-08-22 11:03:34 +020090
Andy Ross65649742019-08-06 13:34:31 -070091/* True if a syscall function must trap to the kernel, usually a
92 * compile-time decision.
93 */
94static ALWAYS_INLINE bool z_syscall_trap(void)
95{
Andy Ross643701a2019-08-13 12:58:38 -070096 bool ret = false;
Andy Ross65649742019-08-06 13:34:31 -070097#ifdef CONFIG_USERSPACE
98#if defined(__ZEPHYR_SUPERVISOR__)
Andy Ross643701a2019-08-13 12:58:38 -070099 ret = false;
Andy Ross65649742019-08-06 13:34:31 -0700100#elif defined(__ZEPHYR_USER__)
Andy Ross643701a2019-08-13 12:58:38 -0700101 ret = true;
Andy Ross65649742019-08-06 13:34:31 -0700102#else
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800103 ret = arch_is_user_context();
Andy Ross65649742019-08-06 13:34:31 -0700104#endif
105#endif
Andy Ross643701a2019-08-13 12:58:38 -0700106 return ret;
Andy Ross65649742019-08-06 13:34:31 -0700107}
Andrew Boie0d9a9be2017-09-29 16:51:36 -0700108
Piotr Zięcik78eb7182019-08-22 11:03:34 +0200109/**
110 * Indicate whether the CPU is currently in user mode
111 *
112 * @return true if the CPU is currently running with user permissions
113 */
Daniel Leung7ad00b92021-07-22 13:13:17 -0700114__pinned_func
Anas Nashif0ec37742021-03-27 12:03:18 -0400115static inline bool k_is_user_context(void)
Piotr Zięcik78eb7182019-08-22 11:03:34 +0200116{
117#ifdef CONFIG_USERSPACE
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800118 return arch_is_user_context();
Piotr Zięcik78eb7182019-08-22 11:03:34 +0200119#else
120 return false;
121#endif
122}
123
Andrew Boie0d9a9be2017-09-29 16:51:36 -0700124#ifdef __cplusplus
125}
126#endif
127
Andrew Boiea23c2452017-09-13 18:04:21 -0700128#endif /* _ASMLANGUAGE */
129
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700130#endif