native_internal.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2013-2014 Ludwig Knüpfer
3  * SPDX-FileCopyrightText: 2025 carl-tud
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
35 #include "util/ucontext.h"
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <poll.h>
39 
40 #include <stdbool.h>
41 #include <netdb.h>
42 #include <ifaddrs.h>
43 #include <time.h>
44 #include <sys/time.h>
45 #include <sys/stat.h>
46 #include <sys/statvfs.h>
47 #include <sys/uio.h>
48 #include <dirent.h>
49 
50 #include "cpu_conf.h"
51 #include "thread.h"
52 #include "sched.h"
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 #include "syscalls.h"
59 
60 /* MARK: - Internal native CPU API */
69 typedef void (*_native_callback_t)(void);
70 
74 void native_cpu_init(void);
77 /* MARK: - Native Signal Handling */
87 extern volatile bool _native_interrupts_enabled;
88 
93 extern sigset_t _native_sig_set;
94 
101 extern int _signal_pipe_fd[2];
102 
107 extern volatile int _native_pending_signals;
108 
113 
123 
132 
142 void _native_call_sig_handlers_and_switch(void);
143 
149 extern void _native_sig_leave_tramp(void);
152 /* MARK: - System Calls */
163 extern volatile int _native_pending_syscalls;
164 
168 static inline void _native_pending_syscalls_up(void) {
169  _native_pending_syscalls += 1;
170 }
171 
175 static inline void _native_pending_syscalls_down(void) {
176  _native_pending_syscalls -= 1;
177 }
178 
189 void _native_syscall_leave(void);
190 
197 void _native_syscall_enter(void);
198 
207 /* MARK: - Native Context Switching */
216 extern volatile uintptr_t _native_user_fptr;
217 
221 extern volatile int _native_in_isr;
222 
227 
231 extern ucontext_t *_native_isr_context;
232 
240 extern void _native_isr_leave(void);
241 
247 static inline void _native_isr_context_make(void (*func)(void)) {
248  _native_isr_context->uc_stack.ss_sp = _isr_stack;
249  _native_isr_context->uc_stack.ss_size = sizeof(_isr_stack);
250  _native_isr_context->uc_stack.ss_flags = 0;
251 
252  /* Create the ISR context, will execute _isr_schedule_and_switch */
253  makecontext(_native_isr_context, func, 0);
254 }
255 
260 static inline ucontext_t* _native_user_context(void) {
261  /* Use intermediate cast to uintptr_t to silence -Wcast-align.
262  * stacks are manually word aligned in thread_static_init() */
263  return (ucontext_t *)(uintptr_t)thread_get_active()->sp;
264 }
267 /* MARK: - Native Process State */
276 extern const char *_progname;
277 
282 extern char **_native_argv;
283 
288 extern pid_t _native_pid;
289 
294 extern pid_t _native_id;
295 
300 extern unsigned int _native_rng_seed;
301 
309 extern int _native_rng_mode;
312 /* MARK: - Native Read/Write Methods */
320 ssize_t _native_read(int fd, void *buf, size_t count);
321 
325 ssize_t _native_write(int fd, const void *buf, size_t count);
326 
330 ssize_t _native_writev(int fildes, const struct iovec *iov, int iovcnt);
333 #ifdef __cplusplus
334 }
335 #endif
336 
static thread_t * thread_get_active(void)
Returns a pointer to the Thread Control Block of the currently running thread.
Definition: thread.h:410
sigset_t _native_sig_set
Signal set during "IRQs enabled".
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.
static ucontext_t * _native_user_context(void)
Retrieves user context.
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.
ucontext_t * _native_isr_context
ISR context.
Scheduler API definition.
POSIX compatible sys/statvfs.h definitions.
Implementation specific CPU configuration options.
char * sp
thread's stack pointer
Definition: thread.h:168
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.
Definition: thread_config.h:39
libc header for scatter/gather I/O