- Implemented first part of signal support.
[planlOS.git] / system / kernel / ke / process.c
blob87f6470718134e75733a20549d1178504aaa265d
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "ke/process.h"
23 #include "ke/thread.h"
24 #include "ke/level.h"
25 #include "ke/cpu.h"
26 #include "ke/interrupts.h"
27 #include "ke/debug.h"
28 #include "sys/syscall.h"
29 #include <stdlib.h>
30 #include <string.h>
31 #include <planlos/signals.h>
33 static uint32_t lastpid = 0;
35 KeSpinlock process_lock;
36 KeProcess **processes = 0;
37 uint32_t processcount = 0;
39 int keInitProcessManager(void)
41 // Create signal entry
42 uintptr_t paddr = mmAllocPhysicalMemory(0, 0, 0x1000);
43 mmMapKernelMemory(paddr, 0xFFFFF000,
44 MM_MAP_READ | MM_MAP_WRITE | MM_MAP_USER);
45 extern void *_binary_signal_o_start;
46 extern void *_binary_signal_o_end;
47 memcpy((void*)0xFFFFF000, &_binary_signal_o_start,
48 (uintptr_t)&_binary_signal_o_end - (uintptr_t)&_binary_signal_o_start);
50 // Create process list
51 processes = 0;
52 processcount = 0;
53 keInitSpinlock(&process_lock);
54 return 0;
57 KeProcess *keCreateProcess(void)
59 // Create empty process
60 KeProcess *newprocess = malloc(sizeof(KeProcess));
61 memset(newprocess, 0, sizeof(KeProcess));
62 newprocess->fd = malloc(sizeof(FsFileHandle*) * 3);
63 memset(newprocess->fd, 0, sizeof(FsFileHandle*) * 3);
64 newprocess->fdcount = 3;
65 newprocess->cwd = strdup("/");
66 int status = mmCreateAddressSpace(&newprocess->memory);
67 if (status)
69 free(newprocess);
70 return 0;
72 keInitSpinlock(&newprocess->lock);
73 newprocess->pid = ++lastpid;
74 // Add process to process list
75 keLockSpinlock(&process_lock);
76 processes = realloc(processes, (processcount + 1) * sizeof(KeProcess*));
77 processes[processcount] = newprocess;
78 processcount++;
79 // Lock process so that caller can use it safely
80 //keLockSpinlock(&newprocess->lock);
81 keUnlockSpinlock(&process_lock);
82 return newprocess;
84 int keTerminateProcess(KeProcess *process)
86 if (process->waitthread)
88 // Destroy process
89 process->waitthread->status = KE_THREAD_RUNNING;
90 return keDestroyProcess(process);
92 else
94 // Kill threads
95 while (process->threadcount > 0)
97 keDestroyThread(process->threads[0]);
99 process->zombie = 1;
100 return 0;
103 int keDestroyProcess(KeProcess *process)
105 // Kill threads
106 while (process->threadcount > 0)
108 keDestroyThread(process->threads[0]);
110 // Remove process from list
111 keLockSpinlock(&process_lock);
112 uint32_t i;
113 for (i = 0; i < processcount; i++)
115 if (processes[i] == process)
117 memmove(&processes[i], &processes[i + 1],
118 (processcount - i - 1) * sizeof(KeProcess*));
119 processcount--;
120 processes = realloc(processes, processcount * sizeof(KeProcess*));
121 break;
124 // Destroy address space
125 mmDestroyAddressSpace(&process->memory);
126 // Delete process
127 free(process->cwd);
128 free(process);
129 keUnlockSpinlock(&process_lock);
130 return 0;
133 int keClearProcess(KeProcess *process, KeThread *current)
135 // Kill threads
136 while (process->threadcount > 1)
138 if (process->threads[0] == current)
139 keDestroyThread(process->threads[1]);
140 else
141 keDestroyThread(process->threads[0]);
143 // Clear address space
144 KeExecLevel oldlevel = keSetExecutionLevel(KE_LEVEL_HIGH);
145 keLockSpinlock(mmGetMemoryLock());
146 mmClearAddressSpace(&process->memory);
147 keUnlockSpinlock(mmGetMemoryLock());
148 keSetExecutionLevel(oldlevel);
149 return 0;
152 KeProcess *keGetProcess(uint32_t pid)
154 keLockSpinlock(&process_lock);
155 uint32_t i;
156 for (i = 0; i < processcount; i++)
158 if (processes[i]->pid == pid)
160 KeProcess *process = processes[i];
161 keUnlockSpinlock(&process_lock);
162 return process;
165 keUnlockSpinlock(&process_lock);
166 return 0;
169 void keSendSignal(KeProcess *process, KeThread *thread, int signal)
171 KeCPU *cpu = keGetCurrentCPU();
172 if (signal == SIGKILL)
174 // Kill process
175 KeThread *currentthread = cpu->currentthread;
176 keTerminateProcess(thread->process);
177 if (currentthread->process == process)
178 asm volatile("int $0x32");
179 return;
181 // TODO: SIGSTOP
182 // Find and lock target thread
183 if (!thread)
185 if (process->threadcount == 0) return;
186 thread = process->threads[0];
188 if (thread)
190 if (thread != cpu->currentthread)
192 if (keTryLockSpinlock(&thread->active))
194 // Someone uses the thread right now
195 thread->status = KE_THREAD_INUSE;
196 keLockSpinlock(&thread->active);
197 if (thread->status == KE_THREAD_INUSE)
199 thread->status = KE_THREAD_RUNNING;
204 // Send signal
205 uintptr_t handler = process->signal_handlers[signal];
206 if (thread && handler && thread->userframe)
208 // Reset signal handler in case the handler is broken
209 process->signal_handlers[signal] = 0;
210 // Get user stack
211 IntStackFrame *userframe = (IntStackFrame*)thread->userframe;
212 uint32_t *stack = sysMapUserMemory(thread, userframe->esp - 16, 16, 1);
213 if (!stack)
215 // TODO
216 kePrint("User stack broken.\n");
217 if (thread != cpu->currentthread)
219 keUnlockSpinlock(&thread->active);
221 keTerminateProcess(thread->process);
222 if (cpu->currentthread->process == process)
223 asm volatile("int $0x32");
224 return;
226 else
228 // Push eip, ebp, signal and handler onto the stack
229 stack[3] = userframe->eip;
230 stack[2] = userframe->ebp;
231 userframe->ebp = userframe->esp - 8;
232 stack[1] = signal;
233 stack[0] = handler;
234 userframe->esp = userframe->esp - 16;
235 userframe->eip = 0xFFFFF000;
238 else
240 // TODO: Default actions
242 // Unlock thread again
243 if (thread != cpu->currentthread)
245 keUnlockSpinlock(&thread->active);