Loading...
Searching...
No Matches
native_internal.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013, 2014 Ludwig Knüpfer
3 * Copyright (C) 2025 carl-tud
4 *
5 * This file is subject to the terms and conditions of the GNU Lesser
6 * General Public License v2.1. See the file LICENSE in the top level
7 * directory for more details.
8 */
9
10#pragma once
11
25
30
37
38#include "util/ucontext.h"
39#include <stdio.h>
40#include <stdint.h>
41#include <poll.h>
42
43#include <stdbool.h>
44#include <netdb.h>
45#include <ifaddrs.h>
46#include <time.h>
47#include <sys/time.h>
48#include <sys/stat.h>
49#include <sys/statvfs.h>
50#include <sys/uio.h>
51#include <dirent.h>
52
53#include "cpu_conf.h"
54#include "thread.h"
55#include "sched.h"
56
57#ifdef __cplusplus
58extern "C" {
59#endif
60
61#include "syscalls.h"
62
63/* MARK: - Internal native CPU API */
68
72typedef void (*_native_callback_t)(void);
73
77void native_cpu_init(void);
79
80/* MARK: - Native Signal Handling */
85
90extern volatile bool _native_interrupts_enabled;
91
98extern int _signal_pipe_fd[2];
99
104extern volatile int _native_pending_signals;
105
110
120
129
139void _native_call_sig_handlers_and_switch(void);
140
146extern void _native_sig_leave_tramp(void);
148
149/* MARK: - System Calls */
160extern volatile int _native_pending_syscalls;
161
165static inline void _native_pending_syscalls_up(void) {
166 _native_pending_syscalls += 1;
167}
168
172static inline void _native_pending_syscalls_down(void) {
173 _native_pending_syscalls -= 1;
174}
175
186void _native_syscall_leave(void);
187
194void _native_syscall_enter(void);
195
203
204/* MARK: - Native Context Switching */
209
213extern volatile uintptr_t _native_user_fptr;
214
218extern volatile int _native_in_isr;
219
224
228extern ucontext_t *_native_isr_context;
229
237extern void _native_isr_leave(void);
238
244static inline void _native_isr_context_make(void (*func)(void)) {
245 _native_isr_context->uc_stack.ss_sp = _isr_stack;
246 _native_isr_context->uc_stack.ss_size = sizeof(_isr_stack);
247 _native_isr_context->uc_stack.ss_flags = 0;
248
249 /* Create the ISR context, will execute _isr_schedule_and_switch */
250 makecontext(_native_isr_context, func, 0);
251}
252
257static inline ucontext_t* _native_user_context(void) {
258 /* Use intermediate cast to uintptr_t to silence -Wcast-align.
259 * stacks are manually word aligned in thread_static_init() */
260 return (ucontext_t *)(uintptr_t)thread_get_active()->sp;
261}
262
263
264/* MARK: - Native Process State */
273extern const char *_progname;
274
279extern char **_native_argv;
280
285extern pid_t _native_pid;
286
291extern pid_t _native_id;
292
297extern unsigned int _native_rng_seed;
298
306extern int _native_rng_mode;
308
309/* MARK: - Native Read/Write Methods */
317ssize_t _native_read(int fd, void *buf, size_t count);
318
322ssize_t _native_write(int fd, const void *buf, size_t count);
323
327ssize_t _native_writev(int fildes, const struct iovec *iov, int iovcnt);
329
330#ifdef __cplusplus
331}
332#endif
333
static thread_t * thread_get_active(void)
Returns a pointer to the Thread Control Block of the currently running thread.
Definition thread.h:413
void _native_sig_leave_tramp(void)
Switches to ISR context, then enables IRQ and returns to userspace.
static void _native_pending_syscalls_up(void)
Increment spending system call counter.
static void _native_isr_context_make(void(*func)(void))
Makes ISR context so that execution continues at func when the context is applied.
void native_cpu_init(void)
Initializes native CPU.
volatile int _native_in_isr
A boolean variable indicating whether program execution currently takes place in an ISR context.
ssize_t _native_read(int fd, void *buf, size_t count)
Reads file, populates given buffer.
volatile uintptr_t _native_user_fptr
Points to instruction in userspace where RIOT left off and switched to ISR context.
ssize_t _native_write(int fd, const void *buf, size_t count)
Writes given data into file.
void _native_init_syscalls(void)
Registers system calls.
void native_interrupt_init(void)
Registers signal handlers for the native CPU.
int native_register_interrupt(int sig, _native_callback_t handler)
Register interrupt handler handler for interrupt signal.
char _isr_stack[THREAD_STACKSIZE_DEFAULT]
Stack used in ISR context.
ssize_t _native_writev(int fildes, const struct iovec *iov, int iovcnt)
Performs a vectored write operation.
void(* _native_callback_t)(void)
Prototype for native's internal callbacks.
static void _native_pending_syscalls_down(void)
Decrements pending system call counter.
int native_unregister_interrupt(int sig)
Unregister interrupt handler for interrupt signal.
static ucontext_t * _native_user_context(void)
Retrieves user context.
ucontext_t * _native_isr_context
ISR context.
Native CPU configuration.
Scheduler API definition.
POSIX compatible sys/statvfs.h definitions.
char * sp
thread's stack pointer
Definition thread.h:171
Structure for scatter/gather I/O.
Definition uio.h:33
#define THREAD_STACKSIZE_DEFAULT
A reasonable default stack size that will suffice most smaller tasks.
libc header for scatter/gather I/O