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 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 struct sos_process *prev, *next; 055 struct sos_process *prev, *next;
055 }; 056 };
056 057
057 058
058 059
059 static struct sos_process *process_list = NULL 060 static struct sos_process *process_list = NULL;
060 061
061 062
062 063
063 struct sos_kslab_cache *cache_struct_process; 064 struct sos_kslab_cache *cache_struct_process;
064 065
065 066
066 067
067 068
068 069
069 void sos_process_dumplist() 070 void sos_process_dumplist()
070 { 071 {
071 struct sos_thread * cur_thr = sos_thread_get 072 struct sos_thread * cur_thr = sos_thread_get_current();
072 struct sos_process * proc; 073 struct sos_process * proc;
073 int nb_procs; 074 int nb_procs;
074 075
075 sos_bochs_printf("<ps>\n"); 076 sos_bochs_printf("<ps>\n");
076 list_foreach(process_list, proc, nb_procs) 077 list_foreach(process_list, proc, nb_procs)
077 { 078 {
078 struct sos_thread * thr; 079 struct sos_thread * thr;
079 int nb_thrs; 080 int nb_thrs;
080 081
081 sos_bochs_printf(" proc@%p: '%s', %d th 082 sos_bochs_printf(" proc@%p: '%s', %d threads, %d refs\n",
082 proc, proc->name?proc-> 083 proc, proc->name?proc->name:"(none)",
083 proc->nb_threads, proc- 084 proc->nb_threads, proc->ref_cnt);
084 085
085 list_foreach_forward_named(proc->thread_ 086 list_foreach_forward_named(proc->thread_list, thr, nb_thrs,
086 prev_in_proce 087 prev_in_process, next_in_process)
087 { 088 {
088 if (thr == cur_thr) 089 if (thr == cur_thr)
089 sos_bochs_printf(" thr@%p: [CUR 090 sos_bochs_printf(" thr@%p: [CURRENT]\n", thr);
090 else 091 else
091 sos_bochs_printf(" thr@%p: %cst 092 sos_bochs_printf(" thr@%p: %cstate=%d eip=%p\n",
092 thr, 093 thr,
093 sos_cpu_context_i 094 sos_cpu_context_is_in_user_mode(thr->cpu_state)?'u':'k',
094 thr->state, 095 thr->state,
095 (void*)sos_cpu_co 096 (void*)sos_cpu_context_get_PC(thr->cpu_state));
096 } 097 }
097 } 098 }
098 sos_bochs_printf(" ======= %d processes === 099 sos_bochs_printf(" ======= %d processes =======\n", nb_procs);
099 sos_bochs_printf("</ps>\n"); 100 sos_bochs_printf("</ps>\n");
100 } 101 }
101 102
102 103
103 sos_ret_t sos_process_subsystem_setup() 104 sos_ret_t sos_process_subsystem_setup()
104 { 105 {
105 106
106 cache_struct_process = sos_kmem_cache_create 107 cache_struct_process = sos_kmem_cache_create("struct_process",
107 108 sizeof(struct sos_process),
108 109 3,
109 110 0,
110 111 SOS_KSLAB_CREATE_MAP
111 112 | SOS_KSLAB_CREATE_ZERO);
112 if (! cache_struct_process) 113 if (! cache_struct_process)
113 return -SOS_ENOMEM; 114 return -SOS_ENOMEM;
114 115
115 return SOS_OK; 116 return SOS_OK;
116 } 117 }
117 118
118 119
119 struct sos_process *sos_process_create(const c !! 120 struct sos_process *sos_process_create_empty(const char *name)
120 sos_boo <<
121 { 121 {
122 sos_ui32_t flags; 122 sos_ui32_t flags;
123 struct sos_process *proc; 123 struct sos_process *proc;
124 124
125 proc = (struct sos_process*) sos_kmem_cache_ 125 proc = (struct sos_process*) sos_kmem_cache_alloc(cache_struct_process, 0);
126 if (! proc) 126 if (! proc)
127 return NULL; 127 return NULL;
128 128
129 129
130 130
131 131
132 if (do_copy_current_process) !! 132
133 proc->address_space = sos_umem_vmm_duplica !! 133 proc->mm_context = sos_mm_context_create();
134 else !! 134 if (NULL == proc->mm_context)
135 proc->address_space = sos_umem_vmm_create_ <<
136 <<
137 if (NULL == proc->address_space) <<
138 { 135 {
139 136
140 sos_kmem_cache_free((sos_vaddr_t)proc); 137 sos_kmem_cache_free((sos_vaddr_t)proc);
141 return NULL; 138 return NULL;
142 } 139 }
143 140
144 if (!name) 141 if (!name)
145 { 142 {
146 struct sos_thread * cur_thr = sos_thread <<
147 if (do_copy_current_process) <<
148 name = cur_thr->process->name; <<
149 else <<
150 name = "[UNNAMED]"; 143 name = "[UNNAMED]";
151 } 144 }
152 145
153 strzcpy(proc->name, name, SOS_PROCESS_MAX_NA 146 strzcpy(proc->name, name, SOS_PROCESS_MAX_NAMELEN);
154 147
155 148
156 sos_disable_IRQs(flags); 149 sos_disable_IRQs(flags);
157 list_add_tail(process_list, proc); 150 list_add_tail(process_list, proc);
158 sos_restore_IRQs(flags); 151 sos_restore_IRQs(flags);
159 152
160 153
161 proc->ref_cnt = 1; 154 proc->ref_cnt = 1;
162 return proc; 155 return proc;
163 } 156 }
164 157
165 158
166 inline 159 inline
167 sos_ret_t sos_process_ref(struct sos_process * 160 sos_ret_t sos_process_ref(struct sos_process *proc)
168 { 161 {
169 sos_ui32_t flags; 162 sos_ui32_t flags;
170 sos_disable_IRQs(flags); 163 sos_disable_IRQs(flags);
171 proc->ref_cnt ++; 164 proc->ref_cnt ++;
172 sos_restore_IRQs(flags); 165 sos_restore_IRQs(flags);
173 return SOS_OK; 166 return SOS_OK;
174 } 167 }
175 168
176 169
177 sos_count_t sos_process_get_nb_threads(const s 170 sos_count_t sos_process_get_nb_threads(const struct sos_process *proc)
178 { 171 {
179 sos_count_t retval; 172 sos_count_t retval;
180 sos_ui32_t flags; 173 sos_ui32_t flags;
181 sos_disable_IRQs(flags); 174 sos_disable_IRQs(flags);
182 retval = proc->nb_threads; 175 retval = proc->nb_threads;
183 sos_restore_IRQs(flags); 176 sos_restore_IRQs(flags);
184 return retval; 177 return retval;
185 } 178 }
186 179
187 180
188 struct sos_mm_context * 181 struct sos_mm_context *
189 sos_process_get_mm_context(const struct sos_pr 182 sos_process_get_mm_context(const struct sos_process *proc)
190 { 183 {
191 return sos_umem_vmm_get_mm_context(proc->add !! 184 return proc->mm_context;
192 } <<
193 <<
194 <<
195 struct sos_umem_vmm_as * <<
196 sos_process_get_address_space(const struct sos <<
197 { <<
198 return proc->address_space; <<
199 } <<
200 <<
201 <<
202 sos_ret_t sos_process_set_address_space(struct <<
203 struct <<
204 { <<
205 if (proc->address_space) <<
206 { <<
207 sos_ret_t retval = sos_umem_vmm_delete_a <<
208 if (SOS_OK != retval) <<
209 return retval; <<
210 } <<
211 <<
212 proc->address_space = new_as; <<
213 return SOS_OK; <<
214 } 185 }
215 186
216 187
217 188
218 189
219 190
220 191
221 192
222 sos_ret_t sos_process_register_thread(struct s 193 sos_ret_t sos_process_register_thread(struct sos_process *in_proc,
223 struct s 194 struct sos_thread *thr)
224 { 195 {
225 sos_ui32_t flags; 196 sos_ui32_t flags;
226 197
227 198
228 SOS_ASSERT_FATAL(in_proc->ref_cnt > 0); 199 SOS_ASSERT_FATAL(in_proc->ref_cnt > 0);
229 200
230 201
231 202
232 SOS_ASSERT_FATAL(thr->state == SOS_THR_CREAT 203 SOS_ASSERT_FATAL(thr->state == SOS_THR_CREATED);
233 204
234 205
235 thr->process = in_proc; 206 thr->process = in_proc;
236 207
237 208
238 sos_process_ref(in_proc); 209 sos_process_ref(in_proc);
239 210
240 211
241 sos_disable_IRQs(flags); 212 sos_disable_IRQs(flags);
242 list_add_tail_named(in_proc->thread_list, th 213 list_add_tail_named(in_proc->thread_list, thr,
243 prev_in_process, next_in 214 prev_in_process, next_in_process);
244 in_proc->nb_threads ++; 215 in_proc->nb_threads ++;
245 sos_restore_IRQs(flags); 216 sos_restore_IRQs(flags);
246 217
247 return SOS_OK; 218 return SOS_OK;
248 } 219 }
249 220
250 221
251 222
252 223
253 sos_ret_t sos_process_unref(struct sos_process 224 sos_ret_t sos_process_unref(struct sos_process *proc)
254 { 225 {
255 sos_ui32_t flags; 226 sos_ui32_t flags;
256 sos_ret_t retval; 227 sos_ret_t retval;
257 228
258 SOS_ASSERT_FATAL(proc->ref_cnt > 0); 229 SOS_ASSERT_FATAL(proc->ref_cnt > 0);
259 230
260 sos_disable_IRQs(flags); 231 sos_disable_IRQs(flags);
261 proc->ref_cnt --; 232 proc->ref_cnt --;
262 if (proc->ref_cnt > 0) 233 if (proc->ref_cnt > 0)
263 { 234 {
264 sos_restore_IRQs(flags); 235 sos_restore_IRQs(flags);
265 return -SOS_EBUSY; 236 return -SOS_EBUSY;
266 } 237 }
267 list_delete(process_list, proc); 238 list_delete(process_list, proc);
268 sos_restore_IRQs(flags); 239 sos_restore_IRQs(flags);
269 240
270 !! 241
271 retval = sos_umem_vmm_delete_as(proc->addres !! 242 retval = sos_mm_context_unref(proc->mm_context);
272 SOS_ASSERT_FATAL(SOS_OK == retval); 243 SOS_ASSERT_FATAL(SOS_OK == retval);
273 244
274 245
275 sos_kmem_cache_free((sos_vaddr_t)proc); 246 sos_kmem_cache_free((sos_vaddr_t)proc);
276 247
277 return SOS_OK; 248 return SOS_OK;
278 } 249 }
279 250
280 251
281 sos_ret_t sos_process_unregister_thread(struct 252 sos_ret_t sos_process_unregister_thread(struct sos_thread *thr)
282 { 253 {
283 sos_ui32_t flags; 254 sos_ui32_t flags;
284 struct sos_process * in_proc = thr->process; 255 struct sos_process * in_proc = thr->process;
285 256
286 SOS_ASSERT_FATAL(thr->state == SOS_THR_ZOMBI 257 SOS_ASSERT_FATAL(thr->state == SOS_THR_ZOMBIE);
287 258
288 259
289 thr->process = NULL; 260 thr->process = NULL;
290 sos_disable_IRQs(flags); 261 sos_disable_IRQs(flags);
291 list_delete_named(in_proc->thread_list, thr, 262 list_delete_named(in_proc->thread_list, thr,
292 prev_in_process, next_in_p 263 prev_in_process, next_in_process);
293 SOS_ASSERT_FATAL(in_proc->nb_threads > 0); 264 SOS_ASSERT_FATAL(in_proc->nb_threads > 0);
294 in_proc->nb_threads --; 265 in_proc->nb_threads --;
295 sos_restore_IRQs(flags); 266 sos_restore_IRQs(flags);
296 267
297 268
298 sos_process_unref(in_proc); 269 sos_process_unref(in_proc);
299 270
300 return SOS_OK; 271 return SOS_OK;
301 } 272 }
302 273
303 274
304 sos_ret_t sos_process_set_name(struct sos_proc 275 sos_ret_t sos_process_set_name(struct sos_process * proc,
305 const char * ne 276 const char * new_name)
306 { 277 {
307 strzcpy(proc->name, new_name, SOS_PROCESS_MA 278 strzcpy(proc->name, new_name, SOS_PROCESS_MAX_NAMELEN);
308 return SOS_OK; 279 return SOS_OK;
309 } 280 }