SimpleOS

LXR

Navigation



Site hébergé par : enix

The LXR Cross Referencer for SOS

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Article:1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 6.5 ] [ 7 ] [ 7.5 ] [ 8 ] [ 9 ] [ 9.5 ]

Diff markup

Differences between /sos/process.c (Article 7.5) and /sos/process.c (Article 7)


001 /* Copyright (C) 2005 David Decotigny             001 /* Copyright (C) 2005 David Decotigny
002                                                   002 
003    This program is free software; you can redi    003    This program is free software; you can redistribute it and/or
004    modify it under the terms of the GNU Genera    004    modify it under the terms of the GNU General Public License
005    as published by the Free Software Foundatio    005    as published by the Free Software Foundation; either version 2
006    of the License, or (at your option) any lat    006    of the License, or (at your option) any later version.
007                                                   007    
008    This program is distributed in the hope tha    008    This program is distributed in the hope that it will be useful,
009    but WITHOUT ANY WARRANTY; without even the     009    but WITHOUT ANY WARRANTY; without even the implied warranty of
010    MERCHANTABILITY or FITNESS FOR A PARTICULAR    010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
011    GNU General Public License for more details    011    GNU General Public License for more details.
012                                                   012    
013    You should have received a copy of the GNU     013    You should have received a copy of the GNU General Public License
014    along with this program; if not, write to t    014    along with this program; if not, write to the Free Software
015    Foundation, Inc., 59 Temple Place - Suite 3    015    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
016    USA.                                           016    USA. 
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  * Definition of a process in SOS. A process i    034  * Definition of a process in SOS. A process is basically the
035  * collection of all the resources requested b    035  * collection of all the resources requested by a user program. The
036  * threads are one of these resources, the mm_    036  * threads are one of these resources, the mm_context is one other
037  * example of such resources.                     037  * example of such resources.
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   /** First important resource: the address sp !! 043   /** First important resource: the MMU translation tables'
044   struct sos_umem_vmm_as *address_space;       !! 044       configuration */
                                                   >> 045   struct sos_mm_context  *mm_context;
045                                                   046 
046   /** Second important resource: the CPU execu    047   /** Second important resource: the CPU execution contexts, aka the
047       threads executing in the context of this    048       threads executing in the context of this process. */
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   /** Reference counter, including threads */     052   /** Reference counter, including threads */
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 /** The list of processes in the system */        059 /** The list of processes in the system */
059 static struct sos_process *process_list = NULL    060 static struct sos_process *process_list = NULL;
060                                                   061 
061                                                   062 
062 /** The cache for the sos_process structures *    063 /** The cache for the sos_process structures */
063 struct sos_kslab_cache *cache_struct_process;     064 struct sos_kslab_cache *cache_struct_process;
064                                                   065 
065                                                   066 
066 /**                                               067 /**
067  * Helper function for debugging purposes         068  * Helper function for debugging purposes
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   /* Create the cache for the process structur    106   /* Create the cache for the process structures */
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   /* proc is already filled with 0 (cache has     129   /* proc is already filled with 0 (cache has SOS_KSLAB_CREATE_ZERO
130      flag) */                                     130      flag) */
131                                                   131 
132   if (do_copy_current_process)                 !! 132   /* Initialize a new mm_context */
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       /* Error */                                 136       /* Error */
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   /* Add it to the global list of processes */    148   /* Add it to the global list of processes */
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   /* Mark the process as referenced */            153   /* Mark the process as referenced */
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  * Restricted functions                           189  * Restricted functions
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   /* The process is assumed to be referenced b    198   /* The process is assumed to be referenced by somebody */
228   SOS_ASSERT_FATAL(in_proc->ref_cnt > 0);         199   SOS_ASSERT_FATAL(in_proc->ref_cnt > 0);
229                                                   200 
230   /* Only threads that are being created are a    201   /* Only threads that are being created are allowed to be attached to
231      a process */                                 202      a process */
232   SOS_ASSERT_FATAL(thr->state == SOS_THR_CREAT    203   SOS_ASSERT_FATAL(thr->state == SOS_THR_CREATED);
233                                                   204 
234   /* Update the list of the threads in the pro    205   /* Update the list of the threads in the process */
235   thr->process = in_proc;                         206   thr->process = in_proc;
236                                                   207 
237   /* Increment the reference count for the pro    208   /* Increment the reference count for the process */
238   sos_process_ref(in_proc);                       209   sos_process_ref(in_proc);
239                                                   210 
240   /* Add the thread to the process thread's li    211   /* Add the thread to the process thread's list */
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 /** The function responsible for releasing the    222 /** The function responsible for releasing the resources held by the
252     process. */                                   223     process. */
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   /* First: free the user address space */     !! 241   /* First: release the mm_context */
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   /* Free the process structure */                245   /* Free the process structure */
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   /* Update the list of the threads in the pro    259   /* Update the list of the threads in the process */
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   /* Unreference the process */                   268   /* Unreference the process */
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 }
                                                      

source navigation ] diff markup ] identifier search ] general search ]