| 1 | #include <fs/icfs.h> |
|---|
| 2 | #include <sched.h> |
|---|
| 3 | #include <module.h> |
|---|
| 4 | #include <slab.h> |
|---|
| 5 | #include <task.h> |
|---|
| 6 | #include <sys.h> |
|---|
| 7 | #include <i386/process.h> |
|---|
| 8 | #include <malloc.h> |
|---|
| 9 | #include <keobject.h> |
|---|
| 10 | #include <user_acc.h> |
|---|
| 11 | |
|---|
| 12 | LIST_HEAD(processList); |
|---|
| 13 | extern struct ListHead threadList; |
|---|
| 14 | Spinlock procListLock; |
|---|
| 15 | struct Cache* processCache; |
|---|
| 16 | int pid=0; |
|---|
| 17 | |
|---|
| 18 | struct IcAttribute processAttributes[]= |
|---|
| 19 | { |
|---|
| 20 | IC_STRING(struct Process, name, IC_READ), |
|---|
| 21 | IC_INT(struct Process, state, IC_READ), |
|---|
| 22 | IC_INT(struct Process, numFds, IC_READ), |
|---|
| 23 | IC_STRING(struct Process, cmdLine, IC_READ), |
|---|
| 24 | IC_END(), |
|---|
| 25 | }; |
|---|
| 26 | |
|---|
| 27 | KE_OBJECT_TYPE(processType, ThrFreeProcess); |
|---|
| 28 | struct KeSet processSet; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | struct Process* ThrFindProcessById(DWORD pid) |
|---|
| 43 | { |
|---|
| 44 | struct Process* curr; |
|---|
| 45 | int found=0; |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | PreemptDisable(); |
|---|
| 53 | |
|---|
| 54 | ListForEachEntry(curr,&processList,next) |
|---|
| 55 | if (curr->pid == pid) |
|---|
| 56 | { |
|---|
| 57 | found=1; |
|---|
| 58 | break; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | PreemptEnable(); |
|---|
| 62 | |
|---|
| 63 | if (!found) |
|---|
| 64 | return NULL; |
|---|
| 65 | |
|---|
| 66 | return curr; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | struct Process* ThrCreateProcess(char* name) |
|---|
| 82 | { |
|---|
| 83 | struct Process* process; |
|---|
| 84 | |
|---|
| 85 | if (!name) |
|---|
| 86 | return NULL; |
|---|
| 87 | |
|---|
| 88 | process=(struct Process*)MemCacheAlloc(processCache); |
|---|
| 89 | |
|---|
| 90 | if (!process) |
|---|
| 91 | return NULL; |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | if (VirtMemManagerSetup(process)) |
|---|
| 95 | { |
|---|
| 96 | MemCacheFree(processCache, process); |
|---|
| 97 | |
|---|
| 98 | process = NULL; |
|---|
| 99 | |
|---|
| 100 | goto out; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | process->name=(char*)MemAlloc(strlen(name)+1); |
|---|
| 105 | strncpy(process->name, name, strlen(name)); |
|---|
| 106 | |
|---|
| 107 | process->pid = pid++; |
|---|
| 108 | process->cId = 0; |
|---|
| 109 | |
|---|
| 110 | process->state = THR_RUNNING; |
|---|
| 111 | |
|---|
| 112 | SpinLockIrq(&procListLock); |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | process->parent = current; |
|---|
| 116 | |
|---|
| 117 | if (current) |
|---|
| 118 | { |
|---|
| 119 | ThrGetProcess(process); |
|---|
| 120 | ListAddTail(&process->sibling, ¤t->children); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | if (KeObjectAttach(&process->object, "%d", process->pid)) |
|---|
| 126 | { |
|---|
| 127 | ListRemove(&process->sibling); |
|---|
| 128 | MemCacheFree(processCache, process); |
|---|
| 129 | process = NULL; |
|---|
| 130 | goto out; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | IcAddAttributes(&process->object, processAttributes, OffsetOf(struct Process, object)); |
|---|
| 134 | |
|---|
| 135 | ListAddTail(&process->next,&processList); |
|---|
| 136 | |
|---|
| 137 | out: |
|---|
| 138 | |
|---|
| 139 | SpinUnlockIrq(&procListLock); |
|---|
| 140 | return process; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | SYMBOL_EXPORT(ThrCreateProcess); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | void ThrFreeProcess(struct KeObject* object) |
|---|
| 159 | { |
|---|
| 160 | DWORD flags; |
|---|
| 161 | struct Process* process; |
|---|
| 162 | IrqSaveFlags(flags); |
|---|
| 163 | |
|---|
| 164 | process = ContainerOf(object, struct Process, object); |
|---|
| 165 | |
|---|
| 166 | PreemptDisable(); |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | if (process->parent) |
|---|
| 170 | ListRemove(&process->sibling); |
|---|
| 171 | |
|---|
| 172 | ListRemove(&process->next); |
|---|
| 173 | |
|---|
| 174 | PreemptEnable(); |
|---|
| 175 | |
|---|
| 176 | MemFree(process->cmdLine); |
|---|
| 177 | MemFree(process->name); |
|---|
| 178 | MemCacheFree(processCache, process); |
|---|
| 179 | |
|---|
| 180 | IrqRestoreFlags(flags); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | SYMBOL_EXPORT(ThrFreeProcess); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | char* ThrGetProcName(DWORD pid) |
|---|
| 198 | { |
|---|
| 199 | struct Process* curr; |
|---|
| 200 | char* name = "<none>"; |
|---|
| 201 | |
|---|
| 202 | PreemptDisable(); |
|---|
| 203 | |
|---|
| 204 | ListForEachEntry(curr,&processList,next) |
|---|
| 205 | if (curr->pid == pid) |
|---|
| 206 | { |
|---|
| 207 | name = curr->name; |
|---|
| 208 | break; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | PreemptEnable(); |
|---|
| 212 | |
|---|
| 213 | return name; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | int ThrWaitForProcessFinish(int pid, int* finishStatus) |
|---|
| 217 | { |
|---|
| 218 | struct Process* curr; |
|---|
| 219 | int retVal=0, shouldWait; |
|---|
| 220 | |
|---|
| 221 | repeat: |
|---|
| 222 | PreemptDisable(); |
|---|
| 223 | shouldWait = 0; |
|---|
| 224 | ListForEachEntry(curr, ¤t->children, sibling) |
|---|
| 225 | { |
|---|
| 226 | if (pid >= 0 && curr->pid != pid) |
|---|
| 227 | continue; |
|---|
| 228 | |
|---|
| 229 | shouldWait = 1; |
|---|
| 230 | |
|---|
| 231 | if (curr->state == THR_DYING) |
|---|
| 232 | { |
|---|
| 233 | if (finishStatus) |
|---|
| 234 | *finishStatus = (curr->exitCode << 8); |
|---|
| 235 | |
|---|
| 236 | retVal = curr->pid; |
|---|
| 237 | PreemptEnable(); |
|---|
| 238 | ThrPutProcess(curr); |
|---|
| 239 | goto out; |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | PreemptFastEnable(); |
|---|
| 244 | |
|---|
| 245 | if (shouldWait) |
|---|
| 246 | { |
|---|
| 247 | retVal = 0; |
|---|
| 248 | SLEEP_ON(¤t->waitQueue); |
|---|
| 249 | goto repeat; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | retVal = -ENOENT; |
|---|
| 253 | |
|---|
| 254 | out: |
|---|
| 255 | return retVal; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | int SysWaitForProcessFinish(int pid,int* finishStatus) |
|---|
| 273 | { |
|---|
| 274 | if (finishStatus) |
|---|
| 275 | if (VirtCheckArea(finishStatus,sizeof(int),VER_WRITE)) |
|---|
| 276 | return -EFAULT; |
|---|
| 277 | |
|---|
| 278 | return ThrWaitForProcessFinish(pid, finishStatus); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | int SysGetCurrentProcessId() |
|---|
| 294 | { |
|---|
| 295 | return current->pid; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | void ThrProcessConstructor(void* obj) |
|---|
| 299 | { |
|---|
| 300 | struct Process* process=(struct Process*)obj; |
|---|
| 301 | |
|---|
| 302 | ZeroMemory(process,sizeof(struct Process)); |
|---|
| 303 | |
|---|
| 304 | INIT_WAITQUEUE_HEAD(&process->waitQueue); |
|---|
| 305 | INIT_LIST_HEAD(&process->areaList); |
|---|
| 306 | INIT_LIST_HEAD(&process->children); |
|---|
| 307 | KeObjectInit(&process->object, &processSet); |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | static void ThrRelativeAlert() |
|---|
| 324 | { |
|---|
| 325 | struct Process* currProc, *currProc2; |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | ListForEachEntrySafe(currProc, currProc2, ¤t->children, sibling) |
|---|
| 329 | { |
|---|
| 330 | ThrPutProcess(currProc); |
|---|
| 331 | currProc->parent=NULL; |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | if (current->parent) |
|---|
| 338 | WakeUpAll(¤t->parent->waitQueue); |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | void ThrProcessExit(int returnCode) |
|---|
| 354 | { |
|---|
| 355 | struct Thread* curr,*curr2; |
|---|
| 356 | |
|---|
| 357 | SpinLockIrq(&procListLock); |
|---|
| 358 | |
|---|
| 359 | current->state = THR_DYING; |
|---|
| 360 | current->exitCode = returnCode; |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | MmapProcessRemove(current); |
|---|
| 365 | LoadReleaseFsContext(); |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | VirtDestroyMemManager(current->memManager); |
|---|
| 369 | current->memManager=NULL; |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | ThrRelativeAlert(); |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | ThrGetThread(currThread); |
|---|
| 380 | |
|---|
| 381 | ListForEachEntrySafe(curr,curr2,&threadList,list) |
|---|
| 382 | { |
|---|
| 383 | if (curr->parent == current) |
|---|
| 384 | { |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | curr->state=THR_DYING; |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | TimerThreadExit(curr); |
|---|
| 392 | |
|---|
| 393 | ThrReleaseThread(curr); |
|---|
| 394 | ThrArchDestroyThread(curr); |
|---|
| 395 | } |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | ThrPutProcess(current); |
|---|
| 399 | |
|---|
| 400 | current = NULL; |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | SpinUnlockIrq(&procListLock); |
|---|
| 404 | |
|---|
| 405 | ThrSchedule(); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | SYMBOL_EXPORT(ThrProcessExit); |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | int SysExit(int returnCode) |
|---|
| 426 | { |
|---|
| 427 | ThrProcessExit(returnCode); |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | return 0; |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | struct SysCall procSysCallTable[]= |
|---|
| 434 | { |
|---|
| 435 | SysEntry(SysGetCurrentProcessId, 0), |
|---|
| 436 | SysEntry(SysExit, 4), |
|---|
| 437 | SysEntry(SysWaitForProcessFinish, 8), |
|---|
| 438 | SysEntryEnd() |
|---|
| 439 | }; |
|---|
| 440 | |
|---|
| 441 | int ProcessInit() |
|---|
| 442 | { |
|---|
| 443 | processCache=MemCacheCreate("Processes", sizeof(struct Process), ThrProcessConstructor, NULL, 0); |
|---|
| 444 | |
|---|
| 445 | SysRegisterRange(SYS_PROCESS_BASE, procSysCallTable); |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | KeSetCreate(&processSet, NULL, &processType, "Processes"); |
|---|
| 450 | |
|---|
| 451 | return 0; |
|---|
| 452 | } |
|---|