]> pd.if.org Git - zos/blob - process.h
add a readme with a public domain note
[zos] / process.h
1 #ifndef PROCESS_H_
2 #define PROCESS_H_
3
4 #include <stdint.h>
5 #include <stddef.h>
6
7 #include <sys/types.h>
8
9 #include <mem.h>
10
11 /* 8 MB stack */
12 #define RLIMIT_STACK_VAL 0x800000
13
14 #define TASK_KERNEL  0x1
15 #define TASK_NOSCHED 0x2
16
17 /*
18  * memory map for processes
19  * 0xFFFFFFFF8000000 and up, kernel, can copy kernel pml4 entries
20  * program load at 4 MB
21  * stack at 128 TB = 8000 0000 0000 (and down)
22  * mmaps at 127 TB = 7F00 0000 0000 (and down)
23  * heap  at 96  TB = 6000 0000 0000 (and up)
24  * heap at 2 TB?
25  * heap after program load...
26  * program at 4 MB =        40 0000
27  */
28
29 /* so, to create a process,
30  * allocate 2 MB for stack,
31  * allocate whatever is needed for the code
32  * load in the code to the code base
33  * set up the registers
34  * set up the stack for the iretq or sysret
35  * iretq or sysret to start the task
36  */
37  
38 struct registers {
39         uint64_t kerrno;
40         uint64_t rax;
41         uint64_t rbx;
42         uint64_t rcx;
43         uint64_t rdx;
44         uint64_t rdi;
45         uint64_t rsi;
46         uint64_t rsp;
47         uint64_t rbp;
48         uint64_t r8;
49         uint64_t r9;
50         uint64_t r10;
51         uint64_t r11;
52         uint64_t r12;
53         uint64_t r13;
54         uint64_t r14;
55         uint64_t r15;
56         uint64_t rip;
57         uint64_t rflags;
58         uint64_t cr3;
59         uint64_t kernel_stack;
60         uint64_t cs;
61         uint64_t ss;
62         __attribute__((aligned(16))) uint8_t fpuenv[512];
63 } __attribute__((packed));
64
65 /* flags bits */
66
67 /* user or kernel process */
68 #define TM_USER 0x1
69 /* needs re-scheduled */
70 #define TM_SCHEDULE 0x2
71 /* in syscall */
72 #define TM_SYSCALL 0x4
73 /* in interrupt */
74 #define TM_INTERRUPTED 0x8
75
76 #define TM_RUNNABLE 0x10
77
78 struct process {
79         pid_t pid;
80         pid_t ppid;
81
82         /* todo just use a user or kernel process, maybe a flag */
83         int pl; /* privilege level, 0 for kernel, 3 for users */
84         uint64_t flags; /* bit flags */
85
86         struct registers reg;
87         vaddr_t kstack; /* pointer to the kernel stack */
88         vaddr_t stacks;
89         vaddr_t usp; /* user stack pointer */
90         vaddr_t heap;
91         struct process *next, *prev; /* scheduling */
92
93         /* sleep should probably be a struct timespec */
94         uint64_t sleep; /* don't need to sleep for 64 bits of ticks */
95
96         int     status; /* return status from main/exit */
97         unsigned int quantum; /* how many more ticks */
98         // struct inode *cwd;
99         // struct sigaction_t sigaction[NUM_SIGNALS];
100         // uint64_t signal_pending;
101         int (*main)(int ac, char **av);
102 };
103
104 uint64_t getrflags(); /* assembly */
105 void init_tasking();
106  
107 pid_t create_task(struct process *task, void (*main)(), uint64_t rflags, uint64_t pagedir, uint64_t flags);
108 struct process *new_task(void (*entry)(), uint64_t rflags, uint64_t pagedir, uint64_t flags);
109  
110 void sleep(uint32_t ticks);
111 void schedule();
112 void do_schedule();
113 int need_schedule();
114 void preempt(); // Switch task frontend
115 void switch_task(struct registers *old, struct registers *new);
116 void switch_task_iret(struct registers *old, struct registers *new);
117
118 /* in assembly */
119 void usermodetrampoline(void (*main)(), uint64_t rflags);
120 #endif