Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
syscall_handler.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7
8#ifndef ZEPHYR_INCLUDE_SYSCALL_HANDLER_H_
9#define ZEPHYR_INCLUDE_SYSCALL_HANDLER_H_
10
11#ifdef CONFIG_USERSPACE
12
13#ifndef _ASMLANGUAGE
14#include <kernel.h>
15#include <sys/arch_interface.h>
16#include <sys/math_extras.h>
17#include <stdbool.h>
18#include <logging/log.h>
19
20extern const _k_syscall_handler_t _k_syscall_table[K_SYSCALL_LIMIT];
21
22enum _obj_init_check {
23 _OBJ_INIT_TRUE = 0,
24 _OBJ_INIT_FALSE = -1,
25 _OBJ_INIT_ANY = 1
26};
27
49static inline bool z_is_in_user_syscall(void)
50{
51 /* This gets set on entry to the syscall's generasted z_mrsh
52 * function and then cleared on exit. This code path is only
53 * encountered when a syscall is made from user mode, system
54 * calls from supervisor mode bypass everything directly to
55 * the implementation function.
56 */
57 return !k_is_in_isr() && _current->syscall_frame != NULL;
58}
59
81int z_object_validate(struct z_object *ko, enum k_objects otype,
82 enum _obj_init_check init);
83
92extern void z_dump_object_error(int retval, const void *obj,
93 struct z_object *ko, enum k_objects otype);
94
105extern struct z_object *z_object_find(const void *obj);
106
107typedef void (*_wordlist_cb_func_t)(struct z_object *ko, void *context);
108
115extern void z_object_wordlist_foreach(_wordlist_cb_func_t func, void *context);
116
123extern void z_thread_perms_inherit(struct k_thread *parent,
124 struct k_thread *child);
125
132extern void z_thread_perms_set(struct z_object *ko, struct k_thread *thread);
133
140extern void z_thread_perms_clear(struct z_object *ko, struct k_thread *thread);
141
142/*
143 * Revoke access to all objects for the provided thread
144 *
145 * NOTE: Unlike z_thread_perms_clear(), this function will not clear
146 * permissions on public objects.
147 *
148 * @param thread Thread object to revoke access
149 */
150extern void z_thread_perms_all_clear(struct k_thread *thread);
151
160void z_object_uninit(const void *obj);
161
179void z_object_recycle(const void *obj);
180
204static inline size_t z_user_string_nlen(const char *src, size_t maxlen,
205 int *err)
206{
207 return arch_user_string_nlen(src, maxlen, err);
208}
209
225extern void *z_user_alloc_from_copy(const void *src, size_t size);
226
240extern int z_user_from_copy(void *dst, const void *src, size_t size);
241
255extern int z_user_to_copy(void *dst, const void *src, size_t size);
256
271extern char *z_user_string_alloc_copy(const char *src, size_t maxlen);
272
289extern int z_user_string_copy(char *dst, const char *src, size_t maxlen);
290
291#define Z_OOPS(expr) \
292 do { \
293 if (expr) { \
294 arch_syscall_oops(_current->syscall_frame); \
295 } \
296 } while (false)
297
311#define Z_SYSCALL_VERIFY_MSG(expr, fmt, ...) ({ \
312 bool expr_copy = !(expr); \
313 if (expr_copy) { \
314 LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL); \
315 LOG_ERR("syscall %s failed check: " fmt, \
316 __func__, ##__VA_ARGS__); \
317 } \
318 expr_copy; })
319
330#define Z_SYSCALL_VERIFY(expr) Z_SYSCALL_VERIFY_MSG(expr, #expr)
331
348#define Z_SYSCALL_MEMORY(ptr, size, write) \
349 Z_SYSCALL_VERIFY_MSG(arch_buffer_validate((void *)ptr, size, write) \
350 == 0, \
351 "Memory region %p (size %zu) %s access denied", \
352 (void *)(ptr), (size_t)(size), \
353 write ? "write" : "read")
354
368#define Z_SYSCALL_MEMORY_READ(ptr, size) \
369 Z_SYSCALL_MEMORY(ptr, size, 0)
370
384#define Z_SYSCALL_MEMORY_WRITE(ptr, size) \
385 Z_SYSCALL_MEMORY(ptr, size, 1)
386
387#define Z_SYSCALL_MEMORY_ARRAY(ptr, nmemb, size, write) \
388 ({ \
389 size_t product; \
390 Z_SYSCALL_VERIFY_MSG(!size_mul_overflow((size_t)(nmemb), \
391 (size_t)(size), \
392 &product), \
393 "%zux%zu array is too large", \
394 (size_t)(nmemb), (size_t)(size)) || \
395 Z_SYSCALL_MEMORY(ptr, product, write); \
396 })
397
410#define Z_SYSCALL_MEMORY_ARRAY_READ(ptr, nmemb, size) \
411 Z_SYSCALL_MEMORY_ARRAY(ptr, nmemb, size, 0)
412
425#define Z_SYSCALL_MEMORY_ARRAY_WRITE(ptr, nmemb, size) \
426 Z_SYSCALL_MEMORY_ARRAY(ptr, nmemb, size, 1)
427
428static inline int z_obj_validation_check(struct z_object *ko,
429 const void *obj,
430 enum k_objects otype,
431 enum _obj_init_check init)
432{
433 int ret;
434
435 ret = z_object_validate(ko, otype, init);
436
437#ifdef CONFIG_LOG
438 if (ret != 0) {
439 z_dump_object_error(ret, obj, ko, otype);
440 }
441#else
442 ARG_UNUSED(obj);
443#endif
444
445 return ret;
446}
447
448#define Z_SYSCALL_IS_OBJ(ptr, type, init) \
449 Z_SYSCALL_VERIFY_MSG(z_obj_validation_check( \
450 z_object_find((const void *)ptr), \
451 (const void *)ptr, \
452 type, init) == 0, "access denied")
453
464#define Z_SYSCALL_DRIVER_OP(ptr, api_name, op) \
465 ({ \
466 struct api_name *__device__ = (struct api_name *) \
467 ((const struct device *)ptr)->api; \
468 Z_SYSCALL_VERIFY_MSG(__device__->op != NULL, \
469 "Operation %s not defined for driver " \
470 "instance %p", \
471 # op, __device__); \
472 })
473
492#define Z_SYSCALL_SPECIFIC_DRIVER(_device, _dtype, _api) \
493 ({ \
494 const struct device *_dev = (const struct device *)_device; \
495 Z_SYSCALL_OBJ(_dev, _dtype) || \
496 Z_SYSCALL_VERIFY_MSG(_dev->api == _api, \
497 "API structure mismatch"); \
498 })
499
511#define Z_SYSCALL_OBJ(ptr, type) \
512 Z_SYSCALL_IS_OBJ(ptr, type, _OBJ_INIT_TRUE)
513
525#define Z_SYSCALL_OBJ_INIT(ptr, type) \
526 Z_SYSCALL_IS_OBJ(ptr, type, _OBJ_INIT_ANY)
527
541#define Z_SYSCALL_OBJ_NEVER_INIT(ptr, type) \
542 Z_SYSCALL_IS_OBJ(ptr, type, _OBJ_INIT_FALSE)
543
544#include <driver-validation.h>
545
546#endif /* _ASMLANGUAGE */
547
548#endif /* CONFIG_USERSPACE */
549
550#endif /* ZEPHYR_INCLUDE_SYSCALL_HANDLER_H_ */
static struct k_thread thread[2]
Definition: atomic.c:22
void
Definition: eswifi_shell.c:15
size_t arch_user_string_nlen(const char *s, size_t maxsize, int *err)
Safely take the length of a potentially bad string.
bool k_is_in_isr(void)
Determine if code is running at interrupt level.
k_objects
Kernel Object Types.
Definition: kobject.h:27
static ZTEST_BMEM volatile int ret
Definition: k_float_disable.c:28
Extra arithmetic and bit manipulation functions.
Definition: thread.h:201