Tenok
A Linux-like Real-Time Operating System for Robotics and Internet of Things
kernel.h
Go to the documentation of this file.
1 
4 #ifndef __KERNEL_H__
5 #define __KERNEL_H__
6 
7 #include <pthread.h>
8 #include <signal.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <sys/limits.h>
12 #include <sys/resource.h>
13 #include <task.h>
14 
15 #include <common/list.h>
16 #include <common/util.h>
17 #include <fs/fs.h>
18 #include <kernel/kfifo.h>
19 #include <kernel/time.h>
20 #include <kernel/wait.h>
21 
22 #include "kconfig.h"
23 
24 #define DEF_SYSCALL(func, _num) \
25  { \
26  .handler_func = (unsigned long) sys_##func, .num = _num \
27  }
28 
29 #define SYSCALL_ARG(thread, type, idx) *((type *) thread->syscall_args[idx])
30 
31 struct syscall_info {
32  unsigned long handler_func;
33  uint32_t num;
34 };
35 
37  uint32_t func;
38  uint32_t args[4];
39 };
40 
41 enum {
42  THREAD_WAIT,
43  THREAD_READY,
44  THREAD_RUNNING,
45  THREAD_SUSPENDED,
46  THREAD_TERMINATED,
47 } THREAD_STATUS;
48 
49 enum {
50  KERNEL_THREAD = 0,
51  USER_THREAD = 1,
52 } THREAD_TYPE;
53 
54 struct task_struct {
55  uint16_t pid; /* Task ID */
56  struct thread_info *main_thread; /* The main thread belongs to the task */
57 
58  /* For recording for the file descriptors belongs to the task */
59  uint32_t bitmap_fds[BITMAP_SIZE(OPEN_MAX)];
60 
61  /* For recording message queue descriptors belongs to the task */
62  uint32_t bitmap_mqds[BITMAP_SIZE(MQUEUE_MAX)];
63 
64  /* Permission bits withheld from a file the task creates */
65  mode_t umask;
66 
67  struct list_head threads_list; /* List of all threads of the task */
68  struct list_head list; /* Linked to the global task list */
69 };
70 
71 struct thread_info {
72  /* Task */
73  struct task_struct *task; /* Task of the thread belongs to */
74 
75  /* Stack */
76  unsigned long *stack_top; /* Stack pointer to the top of the thread stack */
77  unsigned long stack_top_preserved; /* Preserve for staging new handler */
78  unsigned long *stack; /* Base address of the thread stack */
79  size_t stack_size; /* Stack size of the thread in bytes */
80 
81  /* Syscall */
82  unsigned long *syscall_args[4]; /* Pointer to the syscall arguments */
83  unsigned long *syscall_stack_top;
84  bool syscall_mode;
85  bool syscall_is_timeout; /* Indicate if the syscall waiting time is up */
86  struct timespec syscall_timeout; /* For setting timeout of the syscall */
87 
88  /* Thread */
89  void *retval; /* For passing retval after the thread end */
90  void **retval_join; /* To getting retval from a thread to join */
91  size_t file_request_size; /* Size of the thread requesting to a file */
92  uint32_t sleep_ticks; /* Remained ticks to sleep */
93  uint32_t preempt_cnt; /* For preserving threads's preemption level */
94  uint16_t tid; /* Thread ID */
95  uint16_t timer_cnt; /* The number of timers that the thread has */
96  uint8_t privilege; /* Current execution privilege level */
97  uint8_t status; /* Thread status */
98  uint8_t priority; /* Thread priority */
99  uint8_t original_priority; /* Original priority before inheritance */
100  bool kernel_thread; /* Kernel thread or user thread */
101  bool priority_inherited; /* True if current priority is inherited */
102  bool detached; /* Thread is detached or not */
103  bool joinable; /* Thread is joinable or not */
104  char name[THREAD_NAME_MAX]; /* Thread name */
105  struct thread_once *once_control; /* For handling pthread_once_control */
106 
107  /* Signals */
108  struct sigaction *sig_table[SIGNAL_CNT];
109  struct kfifo signal_queue; /* The queue for pending signals */
110  sigset_t sig_wait_set; /* The set of the signals to wait */
111  uint32_t signal_cnt; /* The number of pending signals in the queue */
112  int *ret_sig; /* For storing retval of the sigwait */
113  siginfo_t *ret_siginfo; /* For storing siginfo of wait */
114  bool wait_for_signal; /* Indicates the thread is waiting for signal */
115 
116  /* Lists */
117  struct list_head timers_list; /* List of timers belongs to the thread */
118  struct list_head poll_files_list; /* List of all files polling for */
119  struct list_head task_list; /* Linked to the task thread list */
120  struct list_head thread_list; /* Linked to the global thread list */
121  struct list_head timeout_list; /* Linked to the global timeout list */
122  struct list_head join_list; /* Linked to another thread waiting for join */
123  struct list_head list; /* Linked to a scheduling list */
124 };
125 
126 #endif
mode_t umask(mode_t mask)
Set the permission bits withheld from the files the task creates.
Definition: file.c:310
Definition: kfifo.h:11
Definition: list.h:111
Definition: signal.h:76
Definition: signal.h:70
Definition: kernel.h:36
Definition: kernel.h:31
Definition: kernel.h:54
Definition: kernel.h:71
Definition: thread.h:22
Definition: time.h:16