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