Diff markup
001 001
002 002
003 003
004 004
005 005
006 006
007 007
008 008
009 009
010 010
011 011
012 012
013 013
014 014
015 015
016 016
017 017
018 018
019 #include <sos/assert.h> 019 #include <sos/assert.h>
020 #include <sos/list.h> 020 #include <sos/list.h>
021 #include <sos/kmem_slab.h> 021 #include <sos/kmem_slab.h>
022 #include <hwcore/irq.h> 022 #include <hwcore/irq.h>
023 #include <drivers/bochs.h> 023 #include <drivers/bochs.h>
024 024
025 #include <sos/umem_vmm.h> 025 #include <sos/umem_vmm.h>
026 026
027 #include "process.h" 027 #include "process.h"
028 028
029 #define SOS_PROCESS_MAX_OPENED_FILES 64 029 #define SOS_PROCESS_MAX_OPENED_FILES 64
030 #define SOS_PROCESS_MAX_NAMELEN 32 030 #define SOS_PROCESS_MAX_NAMELEN 32
031 031
032 032
033 033
034 034
035 035
036 036
037 037
038 038
039 struct sos_process 039 struct sos_process
040 { 040 {
041 char name[SOS_PROCESS_MAX_NAMELEN]; 041 char name[SOS_PROCESS_MAX_NAMELEN];
042 042
043 043
044 struct sos_umem_vmm_as *address_space; 044 struct sos_umem_vmm_as *address_space;
045 045
046 046
047 047
048 struct sos_thread *thread_list; 048 struct sos_thread *thread_list;
049 sos_count_t nb_threads; 049 sos_count_t nb_threads;
050 050
051 051
052 sos_count_t ref_cnt; 052 sos_count_t ref_cnt;
053 053
054 054
055 struct sos_fs_opened_file * fds[SOS_PROCESS_ 055 struct sos_fs_opened_file * fds[SOS_PROCESS_MAX_OPENED_FILES];
056 056
057 057
058 struct sos_fs_opened_file * root; 058 struct sos_fs_opened_file * root;
059 059
060 060
061 struct sos_fs_opened_file * cwd; 061 struct sos_fs_opened_file * cwd;
062 062
063 struct sos_process *prev, *next; 063 struct sos_process *prev, *next;
064 }; 064 };
065 065
066 066
067 067
068 static struct sos_process *process_list = NULL 068 static struct sos_process *process_list = NULL;
069 069
070 070
071 071
072 struct sos_kslab_cache *cache_struct_process; 072 struct sos_kslab_cache *cache_struct_process;
073 073
074 074
075 075
076 076
077 077
078 void sos_process_dumplist() 078 void sos_process_dumplist()
079 { 079 {
080 struct sos_thread * cur_thr = sos_thread_get 080 struct sos_thread * cur_thr = sos_thread_get_current();
081 struct sos_process * proc; 081 struct sos_process * proc;
082 int nb_procs; 082 int nb_procs;
083 083
084 sos_bochs_printf("<ps>\n"); 084 sos_bochs_printf("<ps>\n");
085 list_foreach(process_list, proc, nb_procs) 085 list_foreach(process_list, proc, nb_procs)
086 { 086 {
087 struct sos_thread * thr; 087 struct sos_thread * thr;
088 int nb_thrs; 088 int nb_thrs;
089 089
090 sos_bochs_printf(" proc@%p: '%s', %d th 090 sos_bochs_printf(" proc@%p: '%s', %d threads, %d refs\n",
091 proc, proc->name?proc-> 091 proc, proc->name?proc->name:"(none)",
092 proc->nb_threads, proc- 092 proc->nb_threads, proc->ref_cnt);
093 093
094 list_foreach_forward_named(proc->thread_ 094 list_foreach_forward_named(proc->thread_list, thr, nb_thrs,
095 prev_in_proce 095 prev_in_process, next_in_process)
096 { 096 {
097 if (thr == cur_thr) 097 if (thr == cur_thr)
098 sos_bochs_printf(" thr@%p: [CUR 098 sos_bochs_printf(" thr@%p: [CURRENT]\n", thr);
099 else 099 else
100 sos_bochs_printf(" thr@%p: %cst 100 sos_bochs_printf(" thr@%p: %cstate=%d eip=%p\n",
101 thr, 101 thr,
102 sos_cpu_context_i 102 sos_cpu_context_is_in_user_mode(thr->cpu_state)?'u':'k',
103 thr->state, 103 thr->state,
104 (void*)sos_cpu_co 104 (void*)sos_cpu_context_get_PC(thr->cpu_state));
105 } 105 }
106 } 106 }
107 sos_bochs_printf(" ======= %d processes === 107 sos_bochs_printf(" ======= %d processes =======\n", nb_procs);
108 sos_bochs_printf("</ps>\n"); 108 sos_bochs_printf("</ps>\n");
109 } 109 }
110 110
111 111
112 sos_ret_t sos_process_subsystem_setup() 112 sos_ret_t sos_process_subsystem_setup()
113 { 113 {
114 114
115 cache_struct_process = sos_kmem_cache_create 115 cache_struct_process = sos_kmem_cache_create("struct_process",
116 116 sizeof(struct sos_process),
117 117 3,
118 118 0,
119 119 SOS_KSLAB_CREATE_MAP
120 120 | SOS_KSLAB_CREATE_ZERO);
121 if (! cache_struct_process) 121 if (! cache_struct_process)
122 return -SOS_ENOMEM; 122 return -SOS_ENOMEM;
123 123
124 return SOS_OK; 124 return SOS_OK;
125 } 125 }
126 126
127 127
128 struct sos_process *sos_process_create(const c 128 struct sos_process *sos_process_create(const char *name,
129 sos_boo 129 sos_bool_t do_copy_current_process)
130 { 130 {
131 sos_ret_t retval = SOS_OK; 131 sos_ret_t retval = SOS_OK;
132 sos_ui32_t flags; 132 sos_ui32_t flags;
133 struct sos_process *proc; 133 struct sos_process *proc;
134 134
135 <<
136 proc = (struct sos_process*) sos_kmem_cache_ 135 proc = (struct sos_process*) sos_kmem_cache_alloc(cache_struct_process, 0);
137 if (! proc) 136 if (! proc)
138 return NULL; 137 return NULL;
139 138
140 139
141 140
142 141
143 142
144 if (do_copy_current_process) 143 if (do_copy_current_process)
145 { 144 {
146 struct sos_process * myself = sos_thread 145 struct sos_process * myself = sos_thread_get_current()->process;
147 int fd; 146 int fd;
148 147
149 for (fd = 0 ; fd < SOS_PROCESS_MAX_OPENE 148 for (fd = 0 ; fd < SOS_PROCESS_MAX_OPENED_FILES ; fd++)
150 if (NULL != myself->fds[fd]) 149 if (NULL != myself->fds[fd])
151 { 150 {
152 retval = sos_fs_duplicate_opened_f 151 retval = sos_fs_duplicate_opened_file(myself->fds[fd],
153 152 proc,
154 153 & proc->fds[fd]);
155 if (SOS_OK != retval) 154 if (SOS_OK != retval)
156 goto end_create_proc; 155 goto end_create_proc;
157 } 156 }
158 157
159 retval = sos_fs_duplicate_opened_file(my 158 retval = sos_fs_duplicate_opened_file(myself->root,
160 pr 159 proc,
161 & 160 & proc->root);
162 if (SOS_OK != retval) 161 if (SOS_OK != retval)
163 goto end_create_proc; 162 goto end_create_proc;
164 163
165 retval = sos_fs_duplicate_opened_file(my 164 retval = sos_fs_duplicate_opened_file(myself->cwd,
166 pr 165 proc,
167 & 166 & proc->cwd);
168 if (SOS_OK != retval) 167 if (SOS_OK != retval)
169 goto end_create_proc; 168 goto end_create_proc;
170 } 169 }
171 170
172 if (do_copy_current_process) 171 if (do_copy_current_process)
173 proc->address_space = sos_umem_vmm_duplica 172 proc->address_space = sos_umem_vmm_duplicate_current_thread_as(proc);
174 else 173 else
175 proc->address_space = sos_umem_vmm_create_ 174 proc->address_space = sos_umem_vmm_create_empty_as(proc);
176 175
177 if (NULL == proc->address_space) 176 if (NULL == proc->address_space)
178 { 177 {
179 178
180 retval = -SOS_ENOMEM; 179 retval = -SOS_ENOMEM;
181 goto end_create_proc; 180 goto end_create_proc;
182 } 181 }
183 182
184 if (!name) 183 if (!name)
185 { 184 {
186 struct sos_thread * cur_thr = sos_thread 185 struct sos_thread * cur_thr = sos_thread_get_current();
187 if (do_copy_current_process) 186 if (do_copy_current_process)
188 name = cur_thr->process->name; 187 name = cur_thr->process->name;
189 else 188 else
190 name = "[UNNAMED]"; 189 name = "[UNNAMED]";
191 } 190 }
192 191
193 strzcpy(proc->name, name, SOS_PROCESS_MAX_NA 192 strzcpy(proc->name, name, SOS_PROCESS_MAX_NAMELEN);
194 193
195 194
196 sos_disable_IRQs(flags); 195 sos_disable_IRQs(flags);
197 list_add_tail(process_list, proc); 196 list_add_tail(process_list, proc);
198 sos_restore_IRQs(flags); 197 sos_restore_IRQs(flags);
199 198
200 199
201 proc->ref_cnt = 1; 200 proc->ref_cnt = 1;
202 201
203 end_create_proc: 202 end_create_proc:
204 if (SOS_OK != retval) 203 if (SOS_OK != retval)
205 { 204 {
206 int fd; 205 int fd;
207 206
208 207
209 for (fd = 0 ; fd < SOS_PROCESS_MAX_OPENE 208 for (fd = 0 ; fd < SOS_PROCESS_MAX_OPENED_FILES ; fd++)
210 if (NULL != proc->fds[fd]) 209 if (NULL != proc->fds[fd])
211 { !! 210 sos_fs_close(proc->fds[fd]);
212 sos_fs_close(proc->fds[fd]); <<
213 proc->fds[fd] = NULL; <<
214 } <<
215 211
216 if (proc->root) 212 if (proc->root)
217 sos_fs_close(proc->root); 213 sos_fs_close(proc->root);
218 if (proc->cwd) 214 if (proc->cwd)
219 sos_fs_close(proc->cwd); 215 sos_fs_close(proc->cwd);
220 proc->root = proc->cwd = NULL; <<
221 216
222 sos_kmem_cache_free((sos_vaddr_t) proc); 217 sos_kmem_cache_free((sos_vaddr_t) proc);
223 proc = NULL; 218 proc = NULL;
224 } 219 }
225 220
226 return proc; 221 return proc;
227 } 222 }
228 223
229 224
230 inline 225 inline
231 sos_ret_t sos_process_ref(struct sos_process * 226 sos_ret_t sos_process_ref(struct sos_process *proc)
232 { 227 {
233 sos_ui32_t flags; 228 sos_ui32_t flags;
234 sos_disable_IRQs(flags); 229 sos_disable_IRQs(flags);
235 proc->ref_cnt ++; 230 proc->ref_cnt ++;
236 sos_restore_IRQs(flags); 231 sos_restore_IRQs(flags);
237 return SOS_OK; 232 return SOS_OK;
238 } 233 }
239 234
240 235
241 sos_count_t sos_process_get_nb_threads(const s 236 sos_count_t sos_process_get_nb_threads(const struct sos_process *proc)
242 { 237 {
243 sos_count_t retval; 238 sos_count_t retval;
244 sos_ui32_t flags; 239 sos_ui32_t flags;
245 sos_disable_IRQs(flags); 240 sos_disable_IRQs(flags);
246 retval = proc->nb_threads; 241 retval = proc->nb_threads;
247 sos_restore_IRQs(flags); 242 sos_restore_IRQs(flags);
248 return retval; 243 return retval;
249 } 244 }
250 245
251 246
252 struct sos_mm_context * 247 struct sos_mm_context *
253 sos_process_get_mm_context(const struct sos_pr 248 sos_process_get_mm_context(const struct sos_process *proc)
254 { 249 {
255 return sos_umem_vmm_get_mm_context(proc->add 250 return sos_umem_vmm_get_mm_context(proc->address_space);
256 } 251 }
257 252
258 253
259 struct sos_umem_vmm_as * 254 struct sos_umem_vmm_as *
260 sos_process_get_address_space(const struct sos 255 sos_process_get_address_space(const struct sos_process *proc)
261 { 256 {
262 return proc->address_space; 257 return proc->address_space;
263 } 258 }
264 259
265 260
266 sos_ret_t sos_process_set_address_space(struct 261 sos_ret_t sos_process_set_address_space(struct sos_process *proc,
267 struct 262 struct sos_umem_vmm_as *new_as)
268 { 263 {
269 int fd; 264 int fd;
270 265
271 266
272 for (fd = 0 ; fd < SOS_PROCESS_MAX_OPENED_FI 267 for (fd = 0 ; fd < SOS_PROCESS_MAX_OPENED_FILES ; fd++)
273 if ( (NULL != proc->fds[fd]) 268 if ( (NULL != proc->fds[fd])
274 && (proc->fds[fd]->open_flags & SOS_F !! 269 && (! (proc->fds[fd]->open_flags & SOS_FS_OPEN_KEEPONEXEC)) )
275 { !! 270 sos_fs_close(proc->fds[fd]);
276 sos_fs_close(proc->fds[fd]); <<
277 proc->fds[fd] = NULL; <<
278 } <<
279 271
280 if (proc->address_space) 272 if (proc->address_space)
281 { 273 {
282 sos_ret_t retval = sos_umem_vmm_delete_a 274 sos_ret_t retval = sos_umem_vmm_delete_as(proc->address_space);
283 if (SOS_OK != retval) 275 if (SOS_OK != retval)
284 return retval; 276 return retval;
285 } 277 }
286 278
287 proc->address_space = new_as; 279 proc->address_space = new_as;
288 return SOS_OK; 280 return SOS_OK;
289 } 281 }
290 282
291 283
292 struct sos_fs_opened_file * 284 struct sos_fs_opened_file *
293 sos_process_get_root(const struct sos_process 285 sos_process_get_root(const struct sos_process *proc)
294 { 286 {
295 return proc->root; 287 return proc->root;
296 } 288 }
297 289
298 290
299 struct sos_fs_opened_file * 291 struct sos_fs_opened_file *
300 sos_process_get_cwd(const struct sos_process * 292 sos_process_get_cwd(const struct sos_process *proc)
301 { 293 {
302 return proc->cwd; 294 return proc->cwd;
303 } 295 }
304 296
305 297
306 struct sos_fs_opened_file * 298 struct sos_fs_opened_file *
307 sos_process_get_opened_file(const struct sos_p 299 sos_process_get_opened_file(const struct sos_process *proc,
308 int fd) 300 int fd)
309 { 301 {
310 if ((fd < 0) || (fd >= SOS_PROCESS_MAX_OPENE 302 if ((fd < 0) || (fd >= SOS_PROCESS_MAX_OPENED_FILES))
311 return NULL; 303 return NULL;
312 return proc->fds[fd]; 304 return proc->fds[fd];
313 } 305 }
314 306
315 307
316 sos_ret_t 308 sos_ret_t
317 sos_process_chroot(struct sos_process *proc, 309 sos_process_chroot(struct sos_process *proc,
318 struct sos_fs_opened_file * 310 struct sos_fs_opened_file * new_root,
319 struct sos_fs_opened_file * 311 struct sos_fs_opened_file ** old_root)
320 { 312 {
321 *old_root = proc->root; 313 *old_root = proc->root;
322 proc->root = new_root; 314 proc->root = new_root;
323 315
324 return SOS_OK; 316 return SOS_OK;
325 } 317 }
326 318
327 319
328 sos_ret_t 320 sos_ret_t
329 sos_process_chdir(struct sos_process *proc, 321 sos_process_chdir(struct sos_process *proc,
330 struct sos_fs_opened_file * 322 struct sos_fs_opened_file * new_cwd,
331 struct sos_fs_opened_file ** 323 struct sos_fs_opened_file ** old_cwd)
332 { 324 {
333 *old_cwd = proc->cwd; 325 *old_cwd = proc->cwd;
334 proc->cwd = new_cwd; 326 proc->cwd = new_cwd;
335 327
336 return SOS_OK; 328 return SOS_OK;
337 } 329 }
338 330
339 331
340 sos_ret_t 332 sos_ret_t
341 sos_process_register_opened_file(struct sos_pr 333 sos_process_register_opened_file(struct sos_process *proc,
342 struct sos_fs 334 struct sos_fs_opened_file * of)
343 { 335 {
344 int i; 336 int i;
345 for (i = 0 ; i < SOS_PROCESS_MAX_OPENED_FILE 337 for (i = 0 ; i < SOS_PROCESS_MAX_OPENED_FILES ; i++)
346 if (NULL == proc->fds[i]) 338 if (NULL == proc->fds[i])
347 { 339 {
348 proc->fds[i] = of; 340 proc->fds[i] = of;
349 return i; 341 return i;
350 } 342 }
351 343
352 return -SOS_EMFILE; 344 return -SOS_EMFILE;
353 } 345 }
354 346
355 347
356 sos_ret_t 348 sos_ret_t
357 sos_process_unregister_opened_file(struct sos_ 349 sos_process_unregister_opened_file(struct sos_process *proc,
358 int fd) 350 int fd)
359 { 351 {
360 if ((fd < 0) || (fd >= SOS_PROCESS_MAX_OPENE 352 if ((fd < 0) || (fd >= SOS_PROCESS_MAX_OPENED_FILES))
361 return -SOS_EBADF; 353 return -SOS_EBADF;
362 354
363 proc->fds[fd] = NULL; 355 proc->fds[fd] = NULL;
364 return SOS_OK; 356 return SOS_OK;
365 } 357 }
366 358
367 359
368 360
369 361
370 362
371 363
372 364
373 365
374 sos_ret_t sos_process_register_thread(struct s 366 sos_ret_t sos_process_register_thread(struct sos_process *in_proc,
375 struct s 367 struct sos_thread *thr)
376 { 368 {
377 sos_ui32_t flags; 369 sos_ui32_t flags;
378 370
379 371
380 SOS_ASSERT_FATAL(in_proc->ref_cnt > 0); 372 SOS_ASSERT_FATAL(in_proc->ref_cnt > 0);
381 373
382 374
383 375
384 SOS_ASSERT_FATAL(thr->state == SOS_THR_CREAT 376 SOS_ASSERT_FATAL(thr->state == SOS_THR_CREATED);
385 377
386 378
387 thr->process = in_proc; 379 thr->process = in_proc;
388 380
389 381
390 sos_process_ref(in_proc); 382 sos_process_ref(in_proc);
391 383
392 384
393 sos_disable_IRQs(flags); 385 sos_disable_IRQs(flags);
394 list_add_tail_named(in_proc->thread_list, th 386 list_add_tail_named(in_proc->thread_list, thr,
395 prev_in_process, next_in 387 prev_in_process, next_in_process);
396 in_proc->nb_threads ++; 388 in_proc->nb_threads ++;
397 sos_restore_IRQs(flags); 389 sos_restore_IRQs(flags);
398 390
399 return SOS_OK; 391 return SOS_OK;
400 } 392 }
401 393
402 394
403 395
404 396
405 sos_ret_t sos_process_unref(struct sos_process 397 sos_ret_t sos_process_unref(struct sos_process *proc)
406 { 398 {
407 sos_ui32_t flags; 399 sos_ui32_t flags;
408 sos_ret_t retval; 400 sos_ret_t retval;
409 int fd; 401 int fd;
410 402
411 SOS_ASSERT_FATAL(proc->ref_cnt > 0); 403 SOS_ASSERT_FATAL(proc->ref_cnt > 0);
412 404
413 sos_disable_IRQs(flags); 405 sos_disable_IRQs(flags);
414 proc->ref_cnt --; 406 proc->ref_cnt --;
415 if (proc->ref_cnt > 0) 407 if (proc->ref_cnt > 0)
416 { 408 {
417 sos_restore_IRQs(flags); 409 sos_restore_IRQs(flags);
418 return -SOS_EBUSY; 410 return -SOS_EBUSY;
419 } 411 }
420 list_delete(process_list, proc); 412 list_delete(process_list, proc);
421 sos_restore_IRQs(flags); 413 sos_restore_IRQs(flags);
422 414
423 415
424 for (fd = 0 ; fd < SOS_PROCESS_MAX_OPENED_FI 416 for (fd = 0 ; fd < SOS_PROCESS_MAX_OPENED_FILES ; fd++)
425 if (NULL != proc->fds[fd]) 417 if (NULL != proc->fds[fd])
426 { !! 418 sos_fs_close(proc->fds[fd]);
427 sos_fs_close(proc->fds[fd]); <<
428 proc->fds[fd] = NULL; <<
429 } <<
430 419
431 sos_fs_close(proc->root); 420 sos_fs_close(proc->root);
432 sos_fs_close(proc->cwd); 421 sos_fs_close(proc->cwd);
433 proc->root = proc->cwd = NULL; <<
434 422
435 423
436 retval = sos_umem_vmm_delete_as(proc->addres 424 retval = sos_umem_vmm_delete_as(proc->address_space);
437 SOS_ASSERT_FATAL(SOS_OK == retval); 425 SOS_ASSERT_FATAL(SOS_OK == retval);
438 426
439 427
440 sos_kmem_cache_free((sos_vaddr_t)proc); 428 sos_kmem_cache_free((sos_vaddr_t)proc);
441 429
442 return SOS_OK; 430 return SOS_OK;
443 } 431 }
444 432
445 433
446 sos_ret_t sos_process_unregister_thread(struct 434 sos_ret_t sos_process_unregister_thread(struct sos_thread *thr)
447 { 435 {
448 sos_ui32_t flags; 436 sos_ui32_t flags;
449 struct sos_process * in_proc = thr->process; 437 struct sos_process * in_proc = thr->process;
450 438
451 SOS_ASSERT_FATAL(thr->state == SOS_THR_ZOMBI 439 SOS_ASSERT_FATAL(thr->state == SOS_THR_ZOMBIE);
452 440
453 441
454 thr->process = NULL; 442 thr->process = NULL;
455 sos_disable_IRQs(flags); 443 sos_disable_IRQs(flags);
456 list_delete_named(in_proc->thread_list, thr, 444 list_delete_named(in_proc->thread_list, thr,
457 prev_in_process, next_in_p 445 prev_in_process, next_in_process);
458 SOS_ASSERT_FATAL(in_proc->nb_threads > 0); 446 SOS_ASSERT_FATAL(in_proc->nb_threads > 0);
459 in_proc->nb_threads --; 447 in_proc->nb_threads --;
460 sos_restore_IRQs(flags); 448 sos_restore_IRQs(flags);
461 449
462 450
463 sos_process_unref(in_proc); 451 sos_process_unref(in_proc);
464 452
465 return SOS_OK; 453 return SOS_OK;
466 } 454 }
467 455
468 456
469 sos_ret_t sos_process_set_name(struct sos_proc 457 sos_ret_t sos_process_set_name(struct sos_process * proc,
470 const char * ne 458 const char * new_name)
471 { 459 {
472 strzcpy(proc->name, new_name, SOS_PROCESS_MA 460 strzcpy(proc->name, new_name, SOS_PROCESS_MAX_NAMELEN);
473 return SOS_OK; 461 return SOS_OK;
474 } 462 }