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