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"
26 #include "ke/interrupts.h"
28 #include "sys/syscall.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
53 keInitSpinlock(&process_lock
);
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
);
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
;
79 // Lock process so that caller can use it safely
80 //keLockSpinlock(&newprocess->lock);
81 keUnlockSpinlock(&process_lock
);
84 int keTerminateProcess(KeProcess
*process
)
86 if (process
->waitthread
)
89 process
->waitthread
->status
= KE_THREAD_RUNNING
;
90 return keDestroyProcess(process
);
95 while (process
->threadcount
> 0)
97 keDestroyThread(process
->threads
[0]);
103 int keDestroyProcess(KeProcess
*process
)
106 while (process
->threadcount
> 0)
108 keDestroyThread(process
->threads
[0]);
110 // Remove process from list
111 keLockSpinlock(&process_lock
);
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
*));
120 processes
= realloc(processes
, processcount
* sizeof(KeProcess
*));
124 // Destroy address space
125 mmDestroyAddressSpace(&process
->memory
);
129 keUnlockSpinlock(&process_lock
);
133 int keClearProcess(KeProcess
*process
, KeThread
*current
)
136 while (process
->threadcount
> 1)
138 if (process
->threads
[0] == current
)
139 keDestroyThread(process
->threads
[1]);
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
);
152 KeProcess
*keGetProcess(uint32_t pid
)
154 keLockSpinlock(&process_lock
);
156 for (i
= 0; i
< processcount
; i
++)
158 if (processes
[i
]->pid
== pid
)
160 KeProcess
*process
= processes
[i
];
161 keUnlockSpinlock(&process_lock
);
165 keUnlockSpinlock(&process_lock
);
169 void keSendSignal(KeProcess
*process
, KeThread
*thread
, int signal
)
171 KeCPU
*cpu
= keGetCurrentCPU();
172 if (signal
== SIGKILL
)
175 KeThread
*currentthread
= cpu
->currentthread
;
176 keTerminateProcess(thread
->process
);
177 if (currentthread
->process
== process
)
178 asm volatile("int $0x32");
182 // Find and lock target thread
185 if (process
->threadcount
== 0) return;
186 thread
= process
->threads
[0];
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
;
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;
211 IntStackFrame
*userframe
= (IntStackFrame
*)thread
->userframe
;
212 uint32_t *stack
= sysMapUserMemory(thread
, userframe
->esp
- 16, 16, 1);
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");
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;
234 userframe
->esp
= userframe
->esp
- 16;
235 userframe
->eip
= 0xFFFFF000;
240 // TODO: Default actions
242 // Unlock thread again
243 if (thread
!= cpu
->currentthread
)
245 keUnlockSpinlock(&thread
->active
);