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/syscall.c (Article 9) and /sos/syscall.c (Article 8)


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 #include <sos/thread.h>                           018 #include <sos/thread.h>
019 #include <sos/kmalloc.h>                          019 #include <sos/kmalloc.h>
020 #include <sos/klibc.h>                            020 #include <sos/klibc.h>
021 #include <drivers/bochs.h>                        021 #include <drivers/bochs.h>
022 #include <hwcore/paging.h>                        022 #include <hwcore/paging.h>
023 #include <sos/physmem.h>                          023 #include <sos/physmem.h>
024 #include <sos/umem_vmm.h>                         024 #include <sos/umem_vmm.h>
025 #include <drivers/zero.h>                         025 #include <drivers/zero.h>
026 #include <drivers/mem.h>                          026 #include <drivers/mem.h>
027 #include <sos/binfmt_elf32.h>                     027 #include <sos/binfmt_elf32.h>
028                                                   028 
029 #include <hwcore/cpu_context.h>                   029 #include <hwcore/cpu_context.h>
030 #include <sos/uaccess.h>                          030 #include <sos/uaccess.h>
031 #include "syscall.h"                              031 #include "syscall.h"
032                                                   032 
                                                   >> 033 
033 /**                                               034 /**
034  * THE syscall entry point                        035  * THE syscall entry point
035  */                                               036  */
036 sos_ret_t sos_do_syscall(int syscall_id,          037 sos_ret_t sos_do_syscall(int syscall_id,
037                          const struct sos_cpu_    038                          const struct sos_cpu_state *user_ctxt)
038 {                                                 039 {
039   sos_ret_t retval;                               040   sos_ret_t retval;
040                                                   041 
041   switch(syscall_id)                              042   switch(syscall_id)
042     {                                             043     {
043     case SOS_SYSCALL_ID_EXIT:                     044     case SOS_SYSCALL_ID_EXIT:
044       {                                           045       {
045         unsigned int status;                      046         unsigned int status;
046                                                   047 
047         retval = sos_syscall_get1arg(user_ctxt    048         retval = sos_syscall_get1arg(user_ctxt, & status);
048         if (SOS_OK != retval)                     049         if (SOS_OK != retval)
049           break;                                  050           break;
050         sos_thread_exit();                        051         sos_thread_exit();
051         retval = -SOS_EFATAL; /* Not reached *    052         retval = -SOS_EFATAL; /* Not reached */
052       }                                           053       }
053       break;                                      054       break;
054                                                   055 
055     case SOS_SYSCALL_ID_FORK:                     056     case SOS_SYSCALL_ID_FORK:
056       {                                           057       {
057         struct sos_thread *cur_thr, *new_thr;     058         struct sos_thread *cur_thr, *new_thr;
058         struct sos_process *new_proc;             059         struct sos_process *new_proc;
059                                                   060 
060         cur_thr = sos_thread_get_current();       061         cur_thr = sos_thread_get_current();
061                                                   062 
062         /* Duplicate the current process (and     063         /* Duplicate the current process (and its address space) */
063         new_proc = sos_process_create(NULL, TR    064         new_proc = sos_process_create(NULL, TRUE);
064         if (! new_proc)                           065         if (! new_proc)
065           {                                       066           {
066             retval = -SOS_ENOMEM;                 067             retval = -SOS_ENOMEM;
067             break;                                068             break;
068           }                                       069           }
069                                                   070         
070         /* Create *the* thread in this new pro    071         /* Create *the* thread in this new processs, copy of the
071            current user thread (same registers    072            current user thread (same registers, EXCEPT eax which is
072            set to 0) */                           073            set to 0) */
073         new_thr =                                 074         new_thr =
074           sos_duplicate_user_thread(NULL, new_    075           sos_duplicate_user_thread(NULL, new_proc,
075                                     cur_thr,      076                                     cur_thr,
076                                     user_ctxt,    077                                     user_ctxt,
077                                     0);           078                                     0);
078         if (! new_thr)                            079         if (! new_thr)
079           {                                       080           {
080             sos_process_unref(new_proc);          081             sos_process_unref(new_proc);
081             retval = -SOS_ENOMEM;                 082             retval = -SOS_ENOMEM;
082             break;                                083             break;
083           }                                       084           }
084                                                   085 
085         sos_process_unref(new_proc);              086         sos_process_unref(new_proc);
086                                                   087 
087         /* Return to the "parent" thread with     088         /* Return to the "parent" thread with a value different from
088            0. Unix says it should be the "PID"    089            0. Unix says it should be the "PID" of the child. We don't
089            have such a "PID" notion for now */    090            have such a "PID" notion for now */
090         retval = (sos_ui32_t)new_proc;            091         retval = (sos_ui32_t)new_proc;
091       }                                           092       }
092       break;                                      093       break;
093                                                   094 
094     case SOS_SYSCALL_ID_EXEC:                     095     case SOS_SYSCALL_ID_EXEC:
095       {                                           096       {
096         struct sos_thread *cur_thr, *new_thr;     097         struct sos_thread *cur_thr, *new_thr;
097         struct sos_process * proc;                098         struct sos_process * proc;
098         struct sos_umem_vmm_as *new_as;           099         struct sos_umem_vmm_as *new_as;
099         sos_uaddr_t user_str, ustack, start_ua    100         sos_uaddr_t user_str, ustack, start_uaddr;
100         sos_size_t len;                           101         sos_size_t len;
101         char * str;                               102         char * str;
102                                                   103 
103         cur_thr = sos_thread_get_current();       104         cur_thr = sos_thread_get_current();
104         proc    = cur_thr->process;               105         proc    = cur_thr->process;
105                                                   106 
106         /* Make sure the process has exactly 1    107         /* Make sure the process has exactly 1 thread in it */
107         if (sos_process_get_nb_threads(proc) !    108         if (sos_process_get_nb_threads(proc) != 1)
108           {                                       109           {
109             retval = -SOS_EBUSY;                  110             retval = -SOS_EBUSY;
110             break;                                111             break;
111           }                                       112           }
112                                                   113 
113         /* Get the user arguments */              114         /* Get the user arguments */
114         retval = sos_syscall_get2args(user_ctx    115         retval = sos_syscall_get2args(user_ctxt, & user_str, & len);
115         if (SOS_OK != retval)                     116         if (SOS_OK != retval)
116           break;                                  117           break;
117                                                   118 
118         /* Copy the program name into kernel s    119         /* Copy the program name into kernel sppace */
119         retval = sos_strndup_from_user(& str,     120         retval = sos_strndup_from_user(& str, user_str, len + 1, 0);
120         if (SOS_OK != retval)                     121         if (SOS_OK != retval)
121           {                                       122           {
122             break;                                123             break;
123           }                                       124           }
124                                                   125 
125         /* Create a new empty address space to    126         /* Create a new empty address space to map the program */
126         new_as = sos_umem_vmm_create_empty_as(    127         new_as = sos_umem_vmm_create_empty_as(proc);
127         if (! new_as)                             128         if (! new_as)
128           {                                       129           {
129             sos_kfree((sos_vaddr_t)str);          130             sos_kfree((sos_vaddr_t)str);
130             retval = -SOS_ENOMEM;                 131             retval = -SOS_ENOMEM;
131             break;                                132             break;
132           }                                       133           }
133                                                   134 
134         /* Map the program in it */               135         /* Map the program in it */
135         start_uaddr = sos_binfmt_elf32_map(new    136         start_uaddr = sos_binfmt_elf32_map(new_as, str);
136         if (start_uaddr == (sos_uaddr_t)NULL)     137         if (start_uaddr == (sos_uaddr_t)NULL)
137           {                                       138           {
138             sos_umem_vmm_delete_as(new_as);       139             sos_umem_vmm_delete_as(new_as);
139             sos_kfree((sos_vaddr_t)str);          140             sos_kfree((sos_vaddr_t)str);
140             retval = -SOS_ENOENT;                 141             retval = -SOS_ENOENT;
141             break;                                142             break;
142           }                                       143           }
143                                                   144 
144         /* Allocate space for the user stack (    145         /* Allocate space for the user stack (8MB) */
145 #define SOS_DEFAULT_USER_STACK_SIZE (8 << 20)     146 #define SOS_DEFAULT_USER_STACK_SIZE (8 << 20)
146         ustack = (SOS_PAGING_TOP_USER_ADDRESS     147         ustack = (SOS_PAGING_TOP_USER_ADDRESS - SOS_DEFAULT_USER_STACK_SIZE)
147                     + 1;                          148                     + 1;
148         retval = sos_dev_zero_map(new_as, &ust    149         retval = sos_dev_zero_map(new_as, &ustack, SOS_DEFAULT_USER_STACK_SIZE,
149                                   SOS_VM_MAP_P    150                                   SOS_VM_MAP_PROT_READ | SOS_VM_MAP_PROT_WRITE,
150                                   /* PRIVATE *    151                                   /* PRIVATE */ 0);
151         if (SOS_OK != retval)                     152         if (SOS_OK != retval)
152           {                                       153           {
153             sos_umem_vmm_delete_as(new_as);       154             sos_umem_vmm_delete_as(new_as);
154             sos_kfree((sos_vaddr_t)str);          155             sos_kfree((sos_vaddr_t)str);
155             break;                                156             break;
156           }                                       157           }
157                                                   158 
158         /* Now create the user thread */          159         /* Now create the user thread */
159         new_thr = sos_create_user_thread(NULL,    160         new_thr = sos_create_user_thread(NULL,
160                                          proc,    161                                          proc,
161                                          start    162                                          start_uaddr,
162                                          0, 0,    163                                          0, 0,
163                                          ustac    164                                          ustack + SOS_DEFAULT_USER_STACK_SIZE
164                                            - 4    165                                            - 4,
165                                          SOS_S    166                                          SOS_SCHED_PRIO_TS_LOWEST);
166         if (! new_thr)                            167         if (! new_thr)
167           {                                       168           {
168             sos_umem_vmm_delete_as(new_as);       169             sos_umem_vmm_delete_as(new_as);
169             sos_kfree((sos_vaddr_t)str);          170             sos_kfree((sos_vaddr_t)str);
170             retval = -SOS_ENOMEM;                 171             retval = -SOS_ENOMEM;
171             break;                                172             break;          
172           }                                       173           }
173                                                   174 
174         sos_process_set_name(proc, str);          175         sos_process_set_name(proc, str);
175                                                   176 
176         /* Switch to this address space */        177         /* Switch to this address space */
177         retval = sos_process_set_address_space    178         retval = sos_process_set_address_space(proc,
178                                                   179                                                new_as);
179         if (SOS_OK != retval)                     180         if (SOS_OK != retval)
180           {                                       181           {
181             sos_umem_vmm_delete_as(new_as);       182             sos_umem_vmm_delete_as(new_as);
182             sos_kfree((sos_vaddr_t)str);          183             sos_kfree((sos_vaddr_t)str);
183             break;                                184             break;          
184           }                                       185           }
185                                                   186 
186         /* The current thread must exit now */    187         /* The current thread must exit now */
187         sos_kfree((sos_vaddr_t)str);              188         sos_kfree((sos_vaddr_t)str);
188         sos_thread_exit();                        189         sos_thread_exit();
189         retval = -SOS_EFATAL;                     190         retval = -SOS_EFATAL;
190       }                                           191       }
191       break;                                      192       break;
192                                                   193 
                                                   >> 194     case SOS_SYSCALL_ID_FAKEMMAP:
                                                   >> 195       {
                                                   >> 196         sos_uaddr_t ptr_hint_uaddr;
                                                   >> 197         sos_uaddr_t hint_uaddr;
                                                   >> 198         sos_size_t  length;
                                                   >> 199         sos_ui32_t  prot;
                                                   >> 200         sos_ui32_t  flags;
                                                   >> 201         sos_uaddr_t name_user;
                                                   >> 202         sos_ui32_t  offs64_hi;
                                                   >> 203         sos_ui32_t  offs64_lo;
                                                   >> 204         sos_luoffset_t offset_in_resource;
                                                   >> 205         char        name[256];
                                                   >> 206         struct sos_umem_vmm_as * my_as;
                                                   >> 207         
                                                   >> 208         retval = sos_syscall_get7args(user_ctxt,
                                                   >> 209                                       (unsigned int*)& ptr_hint_uaddr,
                                                   >> 210                                       (unsigned int*)& length,
                                                   >> 211                                       (unsigned int*)& prot,
                                                   >> 212                                       (unsigned int*)& flags,
                                                   >> 213                                       (unsigned int*)& name_user,
                                                   >> 214                                       (unsigned int*)& offs64_hi,
                                                   >> 215                                       (unsigned int*)& offs64_lo);
                                                   >> 216         if (SOS_OK != retval)
                                                   >> 217           break;
                                                   >> 218 
                                                   >> 219         /* Compute 64 bits offset value */
                                                   >> 220         offset_in_resource   = offs64_hi;
                                                   >> 221         offset_in_resource <<= 32;
                                                   >> 222         offset_in_resource  |= offs64_lo;
                                                   >> 223 
                                                   >> 224         retval = sos_memcpy_from_user((sos_vaddr_t)& hint_uaddr,
                                                   >> 225                                       ptr_hint_uaddr,
                                                   >> 226                                       sizeof(hint_uaddr));
                                                   >> 227         if (sizeof(hint_uaddr) != retval)
                                                   >> 228           {
                                                   >> 229             retval = -SOS_EFAULT;
                                                   >> 230             break;
                                                   >> 231           }
                                                   >> 232 
                                                   >> 233         retval = sos_strzcpy_from_user(name, name_user, sizeof(name));
                                                   >> 234         if (SOS_OK != retval)
                                                   >> 235           break;
                                                   >> 236 
                                                   >> 237         my_as
                                                   >> 238           = sos_process_get_address_space(sos_thread_get_current()->process);
                                                   >> 239         if ( (0 == strncmp(name, "/dev/zero", sizeof(name)))
                                                   >> 240              || (0 == strncmp(name, "/dev/null", sizeof(name))) )
                                                   >> 241           retval = sos_dev_zero_map(my_as, & hint_uaddr, length, prot, flags);
                                                   >> 242         else if (0 == strncmp(name, "/dev/mem", sizeof(name)))
                                                   >> 243           retval = sos_dev_physmem_map(my_as, & hint_uaddr, length,
                                                   >> 244                                        offset_in_resource, prot, flags);
                                                   >> 245         else if (0 == strncmp(name, "/dev/kmem", sizeof(name)))
                                                   >> 246           retval = sos_dev_kmem_map(my_as, & hint_uaddr, length,
                                                   >> 247                                     offset_in_resource, prot, flags);
                                                   >> 248         else
                                                   >> 249           retval = -SOS_ENOENT;
                                                   >> 250 
                                                   >> 251         if (SOS_OK == retval)
                                                   >> 252           {
                                                   >> 253             if (sizeof(hint_uaddr)
                                                   >> 254                 != sos_memcpy_to_user(ptr_hint_uaddr,
                                                   >> 255                                       (sos_vaddr_t)& hint_uaddr,
                                                   >> 256                                       sizeof(hint_uaddr)))
                                                   >> 257               {
                                                   >> 258                 sos_umem_vmm_unmap(my_as, hint_uaddr, length);
                                                   >> 259                 retval = -SOS_EFAULT;
                                                   >> 260               }
                                                   >> 261           }
                                                   >> 262 
                                                   >> 263       }
                                                   >> 264       break;
                                                   >> 265 
193     case SOS_SYSCALL_ID_MUNMAP:                   266     case SOS_SYSCALL_ID_MUNMAP:
194       {                                           267       {
195         sos_uaddr_t start_uaddr;                  268         sos_uaddr_t start_uaddr;
196         sos_size_t  size;                         269         sos_size_t  size;
197         struct sos_umem_vmm_as * my_as;           270         struct sos_umem_vmm_as * my_as;
198                                                   271 
199         my_as                                     272         my_as
200           = sos_process_get_address_space(sos_    273           = sos_process_get_address_space(sos_thread_get_current()->process);
201                                                   274 
202         retval = sos_syscall_get2args(user_ctx    275         retval = sos_syscall_get2args(user_ctxt,
203                                       (unsigne    276                                       (unsigned int*)& start_uaddr,
204                                       (unsigne    277                                       (unsigned int*)& size);
205         if (SOS_OK != retval)                     278         if (SOS_OK != retval)
206           break;                                  279           break;
207                                                   280 
208         retval = sos_umem_vmm_unmap(my_as, sta    281         retval = sos_umem_vmm_unmap(my_as, start_uaddr, size);
209       }                                           282       }
210       break;                                      283       break;
211                                                   284 
212     case SOS_SYSCALL_ID_MPROTECT:                 285     case SOS_SYSCALL_ID_MPROTECT:
213       {                                           286       {
214         sos_uaddr_t start_uaddr;                  287         sos_uaddr_t start_uaddr;
215         sos_size_t  size;                         288         sos_size_t  size;
216         sos_ui32_t  new_access_rights;            289         sos_ui32_t  new_access_rights;
217         struct sos_umem_vmm_as * my_as;           290         struct sos_umem_vmm_as * my_as;
218                                                   291 
219         my_as                                     292         my_as
220           = sos_process_get_address_space(sos_    293           = sos_process_get_address_space(sos_thread_get_current()->process);
221                                                   294 
222         retval = sos_syscall_get3args(user_ctx    295         retval = sos_syscall_get3args(user_ctxt,
223                                       (unsigne    296                                       (unsigned int*)& start_uaddr,
224                                       (unsigne    297                                       (unsigned int*)& size,
225                                       (unsigne    298                                       (unsigned int*)& new_access_rights);
226         if (SOS_OK != retval)                     299         if (SOS_OK != retval)
227           break;                                  300           break;
228                                                   301 
229         retval = sos_thread_prepare_user_space    302         retval = sos_thread_prepare_user_space_access(NULL, (sos_vaddr_t)NULL);
230         if (SOS_OK != retval)                     303         if (SOS_OK != retval)
231           break;                                  304           break;
232                                                   305 
233         retval = sos_umem_vmm_chprot(my_as, st    306         retval = sos_umem_vmm_chprot(my_as, start_uaddr, size,
234                                      new_acces    307                                      new_access_rights);
235                                                   308 
236         sos_thread_end_user_space_access();       309         sos_thread_end_user_space_access();
237       }                                           310       }
238       break;                                      311       break;
239                                                   312 
240     case SOS_SYSCALL_ID_MRESIZE:                  313     case SOS_SYSCALL_ID_MRESIZE:
241       {                                           314       {
242         sos_uaddr_t old_uaddr;                    315         sos_uaddr_t old_uaddr;
243         sos_size_t  old_size;                     316         sos_size_t  old_size;
244         sos_uaddr_t *uptr_new_uaddr;              317         sos_uaddr_t *uptr_new_uaddr;
245         sos_uaddr_t new_uaddr;                    318         sos_uaddr_t new_uaddr;
246         sos_size_t  new_size;                     319         sos_size_t  new_size;
247         sos_ui32_t  flags;                        320         sos_ui32_t  flags;
248         struct sos_umem_vmm_as * my_as;           321         struct sos_umem_vmm_as * my_as;
249                                                   322 
250         my_as                                     323         my_as
251           = sos_process_get_address_space(sos_    324           = sos_process_get_address_space(sos_thread_get_current()->process);
252                                                   325 
253         retval = sos_syscall_get5args(user_ctx    326         retval = sos_syscall_get5args(user_ctxt,
254                                       (unsigne    327                                       (unsigned int*)& old_uaddr,
255                                       (unsigne    328                                       (unsigned int*)& old_size,
256                                       (unsigne    329                                       (unsigned int*)& uptr_new_uaddr,
257                                       (unsigne    330                                       (unsigned int*)& new_size,
258                                       (unsigne    331                                       (unsigned int*)& flags);
259         if (SOS_OK != retval)                     332         if (SOS_OK != retval)
260           break;                                  333           break;
261                                                   334 
262         if (sizeof(new_uaddr) != sos_memcpy_fr    335         if (sizeof(new_uaddr) != sos_memcpy_from_user((sos_vaddr_t)& new_uaddr,
263                                                   336                                                       (sos_uaddr_t)
264                                                   337                                                         uptr_new_uaddr,
265                                                   338                                                       sizeof(new_uaddr)))
266           {                                       339           {
267             retval = -SOS_EFAULT;                 340             retval = -SOS_EFAULT;
268             break;                                341             break;
269           }                                       342           }
270                                                   343 
271         retval = sos_thread_prepare_user_space    344         retval = sos_thread_prepare_user_space_access(NULL, (sos_vaddr_t)NULL);
272         if (SOS_OK != retval)                     345         if (SOS_OK != retval)
273           break;                                  346           break;
274                                                   347 
275         retval = sos_umem_vmm_resize(my_as, ol    348         retval = sos_umem_vmm_resize(my_as, old_uaddr, old_size,
276                                      & new_uad    349                                      & new_uaddr, new_size, flags);
277         sos_thread_end_user_space_access();       350         sos_thread_end_user_space_access();
278         if (SOS_OK != retval)                     351         if (SOS_OK != retval)
279           break;                                  352           break;
280                                                   353 
281         if (sizeof(new_uaddr)                     354         if (sizeof(new_uaddr)
282             == sos_memcpy_to_user((sos_uaddr_t    355             == sos_memcpy_to_user((sos_uaddr_t)uptr_new_uaddr,
283                                   (sos_vaddr_t    356                                   (sos_vaddr_t)&new_uaddr,
284                                   sizeof(new_u    357                                   sizeof(new_uaddr)))
285           {                                       358           {
286             retval = -SOS_EFAULT;                 359             retval = -SOS_EFAULT;
287             break;                                360             break;
288           }                                       361           }
289       }                                           362       }
290       break;                                      363       break;
291                                                   364 
292     case SOS_SYSCALL_ID_NEW_THREAD:               365     case SOS_SYSCALL_ID_NEW_THREAD:
293       {                                           366       {
294         sos_uaddr_t start_func;                   367         sos_uaddr_t start_func;
295         sos_ui32_t  start_arg1, start_arg2;       368         sos_ui32_t  start_arg1, start_arg2;
296         sos_size_t  stack_size;                   369         sos_size_t  stack_size;
297         sos_uaddr_t stack_uaddr;                  370         sos_uaddr_t stack_uaddr;
298                                                   371 
299         struct sos_thread * new_thr;              372         struct sos_thread * new_thr;
300         struct sos_umem_vmm_as * my_as;           373         struct sos_umem_vmm_as * my_as;
301                                                   374 
302         my_as                                     375         my_as
303           = sos_process_get_address_space(sos_    376           = sos_process_get_address_space(sos_thread_get_current()->process);
304                                                   377 
305         retval = sos_syscall_get4args(user_ctx    378         retval = sos_syscall_get4args(user_ctxt,
306                                       (unsigne    379                                       (unsigned int*)& start_func,
307                                       (unsigne    380                                       (unsigned int*)& start_arg1,
308                                       (unsigne    381                                       (unsigned int*)& start_arg2,
309                                       (unsigne    382                                       (unsigned int*)& stack_size);
310         if (SOS_OK != retval)                     383         if (SOS_OK != retval)
311           break;                                  384           break;
312                                                   385 
313         if (stack_size <= 0)                      386         if (stack_size <= 0)
314           {                                       387           {
315             retval = -SOS_EINVAL;                 388             retval = -SOS_EINVAL;
316             break;                                389             break;
317           }                                       390           }
318                                                   391 
319         /* Allocate the stack */                  392         /* Allocate the stack */
320         stack_uaddr = 0;                          393         stack_uaddr = 0;
321         stack_size = SOS_PAGE_ALIGN_SUP(stack_    394         stack_size = SOS_PAGE_ALIGN_SUP(stack_size);
322         retval = sos_dev_zero_map(my_as, & sta    395         retval = sos_dev_zero_map(my_as, & stack_uaddr, stack_size,
323                                   SOS_VM_MAP_P    396                                   SOS_VM_MAP_PROT_READ | SOS_VM_MAP_PROT_WRITE,
324                                   /* PRIVATE *    397                                   /* PRIVATE */ 0);
325         if (SOS_OK != retval)                     398         if (SOS_OK != retval)
326           break;                                  399           break;
327                                                   400 
328         /* Now create the user thread */          401         /* Now create the user thread */
329         new_thr = sos_create_user_thread(NULL,    402         new_thr = sos_create_user_thread(NULL,
330                                          sos_t    403                                          sos_thread_get_current()->process,
331                                          start    404                                          start_func,
332                                          start    405                                          start_arg1,
333                                          start    406                                          start_arg2,
334                                          stack    407                                          stack_uaddr + stack_size - 4,
335                                          SOS_S    408                                          SOS_SCHED_PRIO_TS_LOWEST);
336                                                   409 
337         if (! new_thr)                            410         if (! new_thr)
338           {                                       411           {
339             sos_umem_vmm_unmap(my_as, stack_ua    412             sos_umem_vmm_unmap(my_as, stack_uaddr, stack_size);
340             retval = -SOS_ENOMEM;                 413             retval = -SOS_ENOMEM;
341             break;                                414             break;          
342           }                                       415           }
343                                                   416 
344       }                                           417       }
345       break;                                      418       break;
346                                                   419 
347     case SOS_SYSCALL_ID_NANOSLEEP:                420     case SOS_SYSCALL_ID_NANOSLEEP:
348       {                                           421       {
349         struct sos_time delay;                    422         struct sos_time delay;
350                                                   423 
351         retval = sos_syscall_get2args(user_ctx    424         retval = sos_syscall_get2args(user_ctxt,
352                                       (unsigne    425                                       (unsigned int*)& delay.sec,
353                                       (unsigne    426                                       (unsigned int*)& delay.nanosec);
354         if (SOS_OK != retval)                     427         if (SOS_OK != retval)
355           break;                                  428           break;
356                                                   429 
357         retval = sos_thread_sleep(& delay);       430         retval = sos_thread_sleep(& delay);
358       }                                           431       }
359       break;                                      432       break;
360                                                   433 
361     case SOS_SYSCALL_ID_BRK:                      434     case SOS_SYSCALL_ID_BRK:
362       {                                           435       {
363         sos_uaddr_t new_top_heap;                 436         sos_uaddr_t new_top_heap;
364         struct sos_umem_vmm_as * my_as;           437         struct sos_umem_vmm_as * my_as;
365                                                   438 
366         my_as                                     439         my_as
367           = sos_process_get_address_space(sos_    440           = sos_process_get_address_space(sos_thread_get_current()->process);
368                                                   441 
369         retval = sos_syscall_get1arg(user_ctxt    442         retval = sos_syscall_get1arg(user_ctxt,
370                                      (unsigned    443                                      (unsigned int*)& new_top_heap);
371         if (SOS_OK != retval)                     444         if (SOS_OK != retval)
372           break;                                  445           break;
373                                                   446 
374         retval = sos_thread_prepare_user_space    447         retval = sos_thread_prepare_user_space_access(NULL, (sos_vaddr_t)NULL);
375         if (SOS_OK != retval)                     448         if (SOS_OK != retval)
376           break;                                  449           break;
377                                                   450 
378         retval = sos_umem_vmm_brk(my_as, new_t    451         retval = sos_umem_vmm_brk(my_as, new_top_heap);
379         sos_thread_end_user_space_access();       452         sos_thread_end_user_space_access();
380       }                                           453       }
381       break;                                      454       break;
382                                                   455 
383                                                   456       
384       /**                                         457       /**
385        * File system interface                    458        * File system interface
386        */                                         459        */
387     case SOS_SYSCALL_ID_MOUNT:                    460     case SOS_SYSCALL_ID_MOUNT:
388       {                                           461       {
389         sos_uaddr_t user_src;                     462         sos_uaddr_t user_src;
390         sos_size_t srclen;                        463         sos_size_t srclen;
391         char * kernel_src = NULL;              !! 464         const char * kernel_src = NULL;
392         sos_uaddr_t user_dst;                     465         sos_uaddr_t user_dst;
393         sos_size_t dstlen;                        466         sos_size_t dstlen;
394         char * kernel_dst;                     !! 467         const char * kernel_dst;
395         sos_ui32_t mountflags;                    468         sos_ui32_t mountflags;
396         sos_uaddr_t user_fstype;                  469         sos_uaddr_t user_fstype;
397         char * kernel_fstype;                     470         char * kernel_fstype;
398         sos_uaddr_t user_args;                    471         sos_uaddr_t user_args;
399         char * kernel_args = NULL;                472         char * kernel_args = NULL;
400         struct sos_process * proc;                473         struct sos_process * proc;
401                                                   474 
402         proc = sos_thread_get_current()->proce    475         proc = sos_thread_get_current()->process;
403         retval = sos_syscall_get7args(user_ctx    476         retval = sos_syscall_get7args(user_ctxt,
404                                       (unsigne    477                                       (unsigned int*)& user_src,
405                                       (unsigne    478                                       (unsigned int*)& srclen,
406                                       (unsigne    479                                       (unsigned int*)& user_dst,
407                                       (unsigne    480                                       (unsigned int*)& dstlen,
408                                       (unsigne    481                                       (unsigned int*)& user_fstype,
409                                       (unsigne    482                                       (unsigned int*)& mountflags,
410                                       (unsigne    483                                       (unsigned int*)& user_args);
411         if (SOS_OK != retval)                     484         if (SOS_OK != retval)
412           break;                                  485           break;
413                                                   486 
414         if (user_src != (sos_uaddr_t)NULL)        487         if (user_src != (sos_uaddr_t)NULL)
415           {                                       488           {
416             retval = sos_strndup_from_user(&ke !! 489             retval = sos_memdup_from_user((sos_vaddr_t*) &kernel_src, user_src, srclen, 0);
417             if (SOS_OK != retval)                 490             if (SOS_OK != retval)
418               break;                              491               break;
419           }                                       492           }
420                                                   493 
421         retval = sos_strndup_from_user(&kernel !! 494         retval = sos_memdup_from_user((sos_vaddr_t*) &kernel_dst, user_dst, dstlen, 0);
422         if (SOS_OK != retval)                     495         if (SOS_OK != retval)
423           {                                       496           {
424             if (kernel_src)                       497             if (kernel_src)
425               sos_kfree((sos_vaddr_t)kernel_sr    498               sos_kfree((sos_vaddr_t)kernel_src);
426             break;                                499             break;
427           }                                       500           }
428                                                   501 
429         retval = sos_strndup_from_user(& kerne    502         retval = sos_strndup_from_user(& kernel_fstype, user_fstype, 256, 0);
430         if (SOS_OK != retval)                     503         if (SOS_OK != retval)
431           {                                       504           {
432             if (kernel_src)                       505             if (kernel_src)
433               sos_kfree((sos_vaddr_t)kernel_sr    506               sos_kfree((sos_vaddr_t)kernel_src);
434             sos_kfree((sos_vaddr_t)kernel_dst)    507             sos_kfree((sos_vaddr_t)kernel_dst);
435             break;                                508             break;
436           }                                       509           }
437                                                   510 
438         if (user_args != (sos_uaddr_t)NULL)       511         if (user_args != (sos_uaddr_t)NULL)
439           {                                       512           {
440             retval = sos_strndup_from_user(& k    513             retval = sos_strndup_from_user(& kernel_args, user_args, 1024, 0);
441             if (SOS_OK != retval)                 514             if (SOS_OK != retval)
442               {                                   515               {
443                 if (kernel_src)                   516                 if (kernel_src)
444                   sos_kfree((sos_vaddr_t)kerne    517                   sos_kfree((sos_vaddr_t)kernel_src);
445                 sos_kfree((sos_vaddr_t)kernel_    518                 sos_kfree((sos_vaddr_t)kernel_dst);
446                 sos_kfree((sos_vaddr_t)kernel_    519                 sos_kfree((sos_vaddr_t)kernel_fstype);
447                 break;                            520                 break;
448               }                                   521               }
449           }                                       522           }
450                                                   523 
451         retval = sos_fs_mount(proc, kernel_src    524         retval = sos_fs_mount(proc, kernel_src, srclen,
452                               kernel_dst, dstl    525                               kernel_dst, dstlen,
453                               kernel_fstype,      526                               kernel_fstype,
454                               mountflags,         527                               mountflags,
455                               kernel_args,        528                               kernel_args,
456                               NULL);              529                               NULL);
457         if (kernel_src)                           530         if (kernel_src)
458           sos_kfree((sos_vaddr_t)kernel_src);     531           sos_kfree((sos_vaddr_t)kernel_src);
459         sos_kfree((sos_vaddr_t)kernel_dst);       532         sos_kfree((sos_vaddr_t)kernel_dst);
460         sos_kfree((sos_vaddr_t)kernel_fstype);    533         sos_kfree((sos_vaddr_t)kernel_fstype);
461         if (kernel_args)                          534         if (kernel_args)
462           sos_kfree((sos_vaddr_t)kernel_args);    535           sos_kfree((sos_vaddr_t)kernel_args);
463       }                                           536       }
464       break;                                      537       break;
465                                                   538 
466     case SOS_SYSCALL_ID_UMOUNT:                   539     case SOS_SYSCALL_ID_UMOUNT:
467       {                                           540       {
468         sos_uaddr_t user_str;                     541         sos_uaddr_t user_str;
469         sos_size_t  len;                          542         sos_size_t  len;
470         char * path;                              543         char * path;
471         struct sos_process * proc;                544         struct sos_process * proc;
472                                                   545         
473         proc = sos_thread_get_current()->proce    546         proc = sos_thread_get_current()->process;
474         retval = sos_syscall_get2args(user_ctx    547         retval = sos_syscall_get2args(user_ctxt,
475                                       (unsigne    548                                       (unsigned int*)& user_str,
476                                       (unsigne    549                                       (unsigned int*)& len);
477         if (SOS_OK != retval)                     550         if (SOS_OK != retval)
478           break;                                  551           break;
479                                                   552         
480         retval = sos_strndup_from_user(&path,  !! 553         retval = sos_memdup_from_user((sos_vaddr_t*) &path, user_str, len, 0);
481         if (SOS_OK != retval)                     554         if (SOS_OK != retval)
482           break;                                  555           break;
483                                                   556         
484         retval = sos_fs_umount(proc,              557         retval = sos_fs_umount(proc,
485                                path, len);        558                                path, len);
486         sos_kfree((sos_vaddr_t)path);             559         sos_kfree((sos_vaddr_t)path);
487       }                                           560       }
488       break;                                      561       break;
489                                                   562 
490     case SOS_SYSCALL_ID_SYNC:                     563     case SOS_SYSCALL_ID_SYNC:
491       {                                           564       {
492         sos_fs_sync_all_fs();                     565         sos_fs_sync_all_fs();
493         retval = SOS_OK;                          566         retval = SOS_OK;
494       }                                           567       }
495       break;                                      568       break;
496                                                   569 
497     case SOS_SYSCALL_ID_VFSTAT64:                 570     case SOS_SYSCALL_ID_VFSTAT64:
498       {                                           571       {
499         sos_uaddr_t user_str;                     572         sos_uaddr_t user_str;
500         sos_size_t  len;                          573         sos_size_t  len;
501         sos_uaddr_t user_vfstat_struct;           574         sos_uaddr_t user_vfstat_struct;
502         struct sos_fs_statfs kernel_vfstat_str    575         struct sos_fs_statfs kernel_vfstat_struct;
503         char * path;                              576         char * path;
504         struct sos_process * proc;                577         struct sos_process * proc;
505                                                   578 
506         proc = sos_thread_get_current()->proce    579         proc = sos_thread_get_current()->process;
507         retval = sos_syscall_get3args(user_ctx    580         retval = sos_syscall_get3args(user_ctxt,
508                                       (unsigne    581                                       (unsigned int*)& user_str,
509                                       (unsigne    582                                       (unsigned int*)& len,
510                                       (unsigne    583                                       (unsigned int*)& user_vfstat_struct);
511         if (SOS_OK != retval)                     584         if (SOS_OK != retval)
512           break;                                  585           break;
513                                                   586 
514         retval = sos_strndup_from_user(&path,  !! 587         retval = sos_memdup_from_user((sos_vaddr_t*) &path, user_str, len, 0);
515         if (SOS_OK != retval)                     588         if (SOS_OK != retval)
516           break;                                  589           break;
517                                                   590 
518         retval = sos_fs_vfstat(proc, path, len    591         retval = sos_fs_vfstat(proc, path, len, & kernel_vfstat_struct);
519         sos_kfree((sos_vaddr_t)path);             592         sos_kfree((sos_vaddr_t)path);
520         if (SOS_OK != retval)                     593         if (SOS_OK != retval)
521           break;                                  594           break;
522                                                   595 
523         if (sizeof(kernel_vfstat_struct)          596         if (sizeof(kernel_vfstat_struct)
524             != sos_memcpy_to_user(user_vfstat_    597             != sos_memcpy_to_user(user_vfstat_struct,
525                                   (sos_vaddr_t    598                                   (sos_vaddr_t) & kernel_vfstat_struct,
526                                   sizeof(kerne    599                                   sizeof(kernel_vfstat_struct)))
527           retval = -SOS_EFAULT;                   600           retval = -SOS_EFAULT;
528       }                                           601       }
529       break;                                      602       break;
530                                                   603 
531     case SOS_SYSCALL_ID_OPEN:                     604     case SOS_SYSCALL_ID_OPEN:
532       {                                           605       {
533         sos_uaddr_t user_str;                     606         sos_uaddr_t user_str;
534         sos_size_t  len;                          607         sos_size_t  len;
535         sos_ui32_t  open_flags;                   608         sos_ui32_t  open_flags;
536         sos_ui32_t  access_rights;                609         sos_ui32_t  access_rights;
537         char * path;                              610         char * path;
538         struct sos_fs_opened_file * of;           611         struct sos_fs_opened_file * of;
539         struct sos_process * proc;                612         struct sos_process * proc;
540                                                   613 
541         proc = sos_thread_get_current()->proce    614         proc = sos_thread_get_current()->process;
542         retval = sos_syscall_get4args(user_ctx    615         retval = sos_syscall_get4args(user_ctxt,
543                                       (unsigne    616                                       (unsigned int*)& user_str,
544                                       (unsigne    617                                       (unsigned int*)& len,
545                                       (unsigne    618                                       (unsigned int*)& open_flags,
546                                       (unsigne    619                                       (unsigned int*)& access_rights);
547         if (SOS_OK != retval)                     620         if (SOS_OK != retval)
548           break;                                  621           break;
549                                                   622 
550         retval = sos_strndup_from_user(&path,  !! 623         retval = sos_memdup_from_user((sos_vaddr_t*) &path, user_str, len, 0);
551         if (SOS_OK != retval)                     624         if (SOS_OK != retval)
552           break;                                  625           break;
553                                                   626 
554         retval = sos_fs_open(proc,                627         retval = sos_fs_open(proc,
555                              path, len,           628                              path, len,
556                              open_flags,          629                              open_flags,
557                              access_rights,       630                              access_rights,
558                              & of);               631                              & of);
559         sos_kfree((sos_vaddr_t)path);             632         sos_kfree((sos_vaddr_t)path);
560         if (SOS_OK != retval)                     633         if (SOS_OK != retval)
561           break;                                  634           break;
562                                                   635 
563         retval = sos_process_register_opened_f    636         retval = sos_process_register_opened_file(proc, of);
564         if (retval < 0)                           637         if (retval < 0)
565           {                                       638           {
566             sos_fs_close(of);                     639             sos_fs_close(of);
567             break;                                640             break;
568           }                                       641           }
569       }                                           642       }
570       break;                                      643       break;
571                                                   644 
572     case SOS_SYSCALL_ID_CLOSE:                    645     case SOS_SYSCALL_ID_CLOSE:
573       {                                           646       {
574         struct sos_fs_opened_file * of;           647         struct sos_fs_opened_file * of;
575         struct sos_process * proc;                648         struct sos_process * proc;
576         int fd;                                   649         int fd;
577                                                   650 
578         proc = sos_thread_get_current()->proce    651         proc = sos_thread_get_current()->process;
579         retval = sos_syscall_get1arg(user_ctxt    652         retval = sos_syscall_get1arg(user_ctxt,
580                                      (unsigned    653                                      (unsigned int*)& fd);
581         if (SOS_OK != retval)                     654         if (SOS_OK != retval)
582           break;                                  655           break;
583                                                   656 
584         of = sos_process_get_opened_file(proc,    657         of = sos_process_get_opened_file(proc, fd);
585         if (NULL == of)                           658         if (NULL == of)
586           {                                       659           {
587             retval = -SOS_EBADF;                  660             retval = -SOS_EBADF;
588             break;                                661             break;
589           }                                       662           }
590                                                   663 
591         retval = sos_process_unregister_opened    664         retval = sos_process_unregister_opened_file(proc, fd);
592         if (SOS_OK != retval)                     665         if (SOS_OK != retval)
593           break;                                  666           break;
594                                                   667 
595         retval = sos_fs_close(of);                668         retval = sos_fs_close(of);
596       }                                           669       }
597       break;                                      670       break;
598                                                   671 
599     case SOS_SYSCALL_ID_READ:                     672     case SOS_SYSCALL_ID_READ:
600       {                                           673       {
601         struct sos_fs_opened_file * of;           674         struct sos_fs_opened_file * of;
602         struct sos_process * proc;                675         struct sos_process * proc;
603         sos_uaddr_t uaddr_buf;                    676         sos_uaddr_t uaddr_buf;
604         sos_uaddr_t uaddr_buflen;                 677         sos_uaddr_t uaddr_buflen;
605         sos_size_t kernel_buflen;                 678         sos_size_t kernel_buflen;
606         int fd;                                   679         int fd;
607                                                   680 
608         proc = sos_thread_get_current()->proce    681         proc = sos_thread_get_current()->process;
609         retval = sos_syscall_get3args(user_ctx    682         retval = sos_syscall_get3args(user_ctxt,
610                                       (unsigne    683                                       (unsigned int*)& fd,
611                                       (unsigne    684                                       (unsigned int*)& uaddr_buf,
612                                       (unsigne    685                                       (unsigned int*)& uaddr_buflen);
613         if (SOS_OK != retval)                     686         if (SOS_OK != retval)
614           break;                                  687           break;
615                                                   688 
616         /* Retrieve the value for "buflen" */     689         /* Retrieve the value for "buflen" */
617         retval = sos_memcpy_from_user((sos_vad    690         retval = sos_memcpy_from_user((sos_vaddr_t)& kernel_buflen,
618                                       uaddr_bu    691                                       uaddr_buflen,
619                                       sizeof(k    692                                       sizeof(kernel_buflen));
620         if (sizeof(kernel_buflen) != retval)      693         if (sizeof(kernel_buflen) != retval)
621           {                                       694           {
622             retval = -SOS_EFAULT;                 695             retval = -SOS_EFAULT;
623             break;                                696             break;
624           }                                       697           }
625                                                   698 
626         of = sos_process_get_opened_file(proc,    699         of = sos_process_get_opened_file(proc, fd);
627         if (NULL == of)                           700         if (NULL == of)
628           {                                       701           {
629             retval = -SOS_EBADF;                  702             retval = -SOS_EBADF;
630             break;                                703             break;
631           }                                       704           }
632                                                   705 
633         /* Do the actual reading */               706         /* Do the actual reading */
634         retval = sos_fs_read(of, uaddr_buf, &     707         retval = sos_fs_read(of, uaddr_buf, & kernel_buflen);
635                                                   708         
636         /* Send successful number of bytes rea    709         /* Send successful number of bytes read to user */
637         sos_memcpy_to_user(uaddr_buflen,          710         sos_memcpy_to_user(uaddr_buflen,
638                            (sos_vaddr_t)& kern    711                            (sos_vaddr_t)& kernel_buflen,
639                            sizeof(kernel_bufle    712                            sizeof(kernel_buflen));
640       }                                           713       }
641       break;                                      714       break;
642                                                   715 
643     case SOS_SYSCALL_ID_READDIR:                  716     case SOS_SYSCALL_ID_READDIR:
644       {                                           717       {
645         struct sos_fs_opened_file * of;           718         struct sos_fs_opened_file * of;
646         struct sos_process * proc;                719         struct sos_process * proc;
647         sos_uaddr_t uaddr_direntry;               720         sos_uaddr_t uaddr_direntry;
648         struct sos_fs_dirent direntry;            721         struct sos_fs_dirent direntry;
649         int fd;                                   722         int fd;
650                                                   723 
651         proc = sos_thread_get_current()->proce    724         proc = sos_thread_get_current()->process;
652         retval = sos_syscall_get2args(user_ctx    725         retval = sos_syscall_get2args(user_ctxt,
653                                       (unsigne    726                                       (unsigned int*)& fd,
654                                       (unsigne    727                                       (unsigned int*)& uaddr_direntry);
655         if (SOS_OK != retval)                     728         if (SOS_OK != retval)
656           break;                                  729           break;
657                                                   730 
658         of = sos_process_get_opened_file(proc,    731         of = sos_process_get_opened_file(proc, fd);
659         if (NULL == of)                           732         if (NULL == of)
660           {                                       733           {
661             retval = -SOS_EBADF;                  734             retval = -SOS_EBADF;
662             break;                                735             break;
663           }                                       736           }
664                                                   737 
665         /* Do the actual readdir */               738         /* Do the actual readdir */
666         retval = sos_fs_readdir(of, & direntry    739         retval = sos_fs_readdir(of, & direntry);
667         if (SOS_OK != retval)                     740         if (SOS_OK != retval)
668           break;                                  741           break;
669                                                   742         
670         /* Send direntry structure to user */     743         /* Send direntry structure to user */
671         if (sizeof(direntry) != sos_memcpy_to_    744         if (sizeof(direntry) != sos_memcpy_to_user(uaddr_direntry,
672                                                   745                                                    (sos_vaddr_t)& direntry,
673                                                   746                                                    sizeof(direntry)))
674           retval = -SOS_EFAULT;                   747           retval = -SOS_EFAULT;
675       }                                           748       }
676       break;                                      749       break;
677                                                   750 
678     case SOS_SYSCALL_ID_WRITE:                    751     case SOS_SYSCALL_ID_WRITE:
679       {                                           752       {
680         struct sos_fs_opened_file * of;           753         struct sos_fs_opened_file * of;
681         struct sos_process * proc;                754         struct sos_process * proc;
682         sos_uaddr_t uaddr_buf;                    755         sos_uaddr_t uaddr_buf;
683         sos_uaddr_t uaddr_buflen;                 756         sos_uaddr_t uaddr_buflen;
684         sos_size_t kernel_buflen;                 757         sos_size_t kernel_buflen;
685         int fd;                                   758         int fd;
686                                                   759 
687         proc = sos_thread_get_current()->proce    760         proc = sos_thread_get_current()->process;
688         retval = sos_syscall_get3args(user_ctx    761         retval = sos_syscall_get3args(user_ctxt,
689                                       (unsigne    762                                       (unsigned int*)& fd,
690                                       (unsigne    763                                       (unsigned int*)& uaddr_buf,
691                                       (unsigne    764                                       (unsigned int*)& uaddr_buflen);
692         if (SOS_OK != retval)                     765         if (SOS_OK != retval)
693           break;                                  766           break;
694                                                   767 
695         /* Retrieve the value for "buflen" */     768         /* Retrieve the value for "buflen" */
696         retval = sos_memcpy_from_user((sos_vad    769         retval = sos_memcpy_from_user((sos_vaddr_t)& kernel_buflen,
697                                       uaddr_bu    770                                       uaddr_buflen,
698                                       sizeof(k    771                                       sizeof(kernel_buflen));
699         if (sizeof(kernel_buflen) != retval)      772         if (sizeof(kernel_buflen) != retval)
700           {                                       773           {
701             retval = -SOS_EFAULT;                 774             retval = -SOS_EFAULT;
702             break;                                775             break;
703           }                                       776           }
704                                                   777 
705         of = sos_process_get_opened_file(proc,    778         of = sos_process_get_opened_file(proc, fd);
706         if (NULL == of)                           779         if (NULL == of)
707           {                                       780           {
708             retval = -SOS_EBADF;                  781             retval = -SOS_EBADF;
709             break;                                782             break;
710           }                                       783           }
711                                                   784 
712         /* Do the actual writing */               785         /* Do the actual writing */
713         retval = sos_fs_write(of, uaddr_buf, &    786         retval = sos_fs_write(of, uaddr_buf, & kernel_buflen);
714                                                   787         
715         /* Send successful number of bytes wri    788         /* Send successful number of bytes written to user */
716         sos_memcpy_to_user(uaddr_buflen,          789         sos_memcpy_to_user(uaddr_buflen,
717                            (sos_vaddr_t)& kern    790                            (sos_vaddr_t)& kernel_buflen,
718                            sizeof(kernel_bufle    791                            sizeof(kernel_buflen));
719       }                                           792       }
720       break;                                      793       break;
721                                                   794 
722     case SOS_SYSCALL_ID_SEEK64:                   795     case SOS_SYSCALL_ID_SEEK64:
723       {                                           796       {
724         struct sos_fs_opened_file * of;           797         struct sos_fs_opened_file * of;
725         struct sos_process * proc;                798         struct sos_process * proc;
726         sos_uaddr_t uaddr_offset;                 799         sos_uaddr_t uaddr_offset;
727         sos_seek_whence_t whence;                 800         sos_seek_whence_t whence;
728         sos_lsoffset_t kernel_offset, result_p    801         sos_lsoffset_t kernel_offset, result_position;
729         int fd;                                   802         int fd;
730                                                   803 
731         proc = sos_thread_get_current()->proce    804         proc = sos_thread_get_current()->process;
732         retval = sos_syscall_get3args(user_ctx    805         retval = sos_syscall_get3args(user_ctxt,
733                                       (unsigne    806                                       (unsigned int*)& fd,
734                                       (unsigne    807                                       (unsigned int*)& uaddr_offset,
735                                       (unsigne    808                                       (unsigned int*)& whence);
736         if (SOS_OK != retval)                     809         if (SOS_OK != retval)
737           break;                                  810           break;
738                                                   811 
739         /* Retrieve the value for "buflen" */     812         /* Retrieve the value for "buflen" */
740         retval = sos_memcpy_from_user((sos_vad    813         retval = sos_memcpy_from_user((sos_vaddr_t)& kernel_offset,
741                                       uaddr_of    814                                       uaddr_offset,
742                                       sizeof(k    815                                       sizeof(kernel_offset));
743         if (sizeof(kernel_offset) != retval)      816         if (sizeof(kernel_offset) != retval)
744           {                                       817           {
745             retval = -SOS_EFAULT;                 818             retval = -SOS_EFAULT;
746             break;                                819             break;
747           }                                       820           }
748                                                   821 
749         of = sos_process_get_opened_file(proc,    822         of = sos_process_get_opened_file(proc, fd);
750         if (NULL == of)                           823         if (NULL == of)
751           {                                       824           {
752             retval = -SOS_EBADF;                  825             retval = -SOS_EBADF;
753             break;                                826             break;
754           }                                       827           }
755                                                   828 
756         /* Do the actual seek */                  829         /* Do the actual seek */
757         retval = sos_fs_seek(of, kernel_offset    830         retval = sos_fs_seek(of, kernel_offset, whence, & result_position);
758                                                   831         
759         /* Send successful number of bytes wri    832         /* Send successful number of bytes written to user */
760         sos_memcpy_to_user(uaddr_offset,          833         sos_memcpy_to_user(uaddr_offset,
761                            (sos_vaddr_t)& resu    834                            (sos_vaddr_t)& result_position,
762                            sizeof(kernel_offse    835                            sizeof(kernel_offset));
763       }                                           836       }
764       break;                                      837       break;
765                                                   838 
766     case SOS_SYSCALL_ID_FTRUNCATE64:              839     case SOS_SYSCALL_ID_FTRUNCATE64:
767       {                                           840       {
768         struct sos_fs_opened_file * of;           841         struct sos_fs_opened_file * of;
769         struct sos_process * proc;                842         struct sos_process * proc;
770         sos_lsoffset_t length;                    843         sos_lsoffset_t length;
771         int fd;                                   844         int fd;
772                                                   845 
773         proc = sos_thread_get_current()->proce    846         proc = sos_thread_get_current()->process;
774         retval = sos_syscall_get2args(user_ctx    847         retval = sos_syscall_get2args(user_ctxt,
775                                       (unsigne    848                                       (unsigned int*)& fd,
776                                       (unsigne    849                                       (unsigned int*)& length);
777         if (SOS_OK != retval)                     850         if (SOS_OK != retval)
778           break;                                  851           break;
779                                                   852 
780         of = sos_process_get_opened_file(proc,    853         of = sos_process_get_opened_file(proc, fd);
781         if (NULL == of)                           854         if (NULL == of)
782           {                                       855           {
783             retval = -SOS_EBADF;                  856             retval = -SOS_EBADF;
784             break;                                857             break;
785           }                                       858           }
786                                                   859 
787         /* Do the actual trunc */                 860         /* Do the actual trunc */
788         retval = sos_fs_ftruncate(of, length);    861         retval = sos_fs_ftruncate(of, length);
789       }                                           862       }
790       break;                                      863       break;
791                                                   864 
792     case SOS_SYSCALL_ID_FSMMAP:                   865     case SOS_SYSCALL_ID_FSMMAP:
793       {                                           866       {
794         sos_uaddr_t ptr_hint_uaddr;               867         sos_uaddr_t ptr_hint_uaddr;
795         sos_uaddr_t hint_uaddr;                   868         sos_uaddr_t hint_uaddr;
796         sos_size_t  length;                       869         sos_size_t  length;
797         sos_ui32_t  prot;                         870         sos_ui32_t  prot;
798         sos_ui32_t  flags;                        871         sos_ui32_t  flags;
799         int         fd;                           872         int         fd;
800         sos_ui32_t  offs64_hi;                    873         sos_ui32_t  offs64_hi;
801         sos_ui32_t  offs64_lo;                    874         sos_ui32_t  offs64_lo;
802         sos_luoffset_t offset_in_resource;        875         sos_luoffset_t offset_in_resource;
803         struct sos_fs_opened_file * of;           876         struct sos_fs_opened_file * of;
804         struct sos_process * proc;                877         struct sos_process * proc;
805                                                   878         
806         proc = sos_thread_get_current()->proce    879         proc = sos_thread_get_current()->process;
807         retval = sos_syscall_get7args(user_ctx    880         retval = sos_syscall_get7args(user_ctxt,
808                                       (unsigne    881                                       (unsigned int*)& ptr_hint_uaddr,
809                                       (unsigne    882                                       (unsigned int*)& length,
810                                       (unsigne    883                                       (unsigned int*)& prot,
811                                       (unsigne    884                                       (unsigned int*)& flags,
812                                       (unsigne    885                                       (unsigned int*)& fd,
813                                       (unsigne    886                                       (unsigned int*)& offs64_hi,
814                                       (unsigne    887                                       (unsigned int*)& offs64_lo);
815         if (SOS_OK != retval)                     888         if (SOS_OK != retval)
816           break;                                  889           break;
817                                                   890 
818         of = sos_process_get_opened_file(proc,    891         of = sos_process_get_opened_file(proc, fd);
819         if (NULL == of)                           892         if (NULL == of)
820           {                                       893           {
821             retval = -SOS_EBADF;                  894             retval = -SOS_EBADF;
822             break;                                895             break;
823           }                                       896           }
824                                                   897 
825         /* Compute 64 bits offset value */        898         /* Compute 64 bits offset value */
826         offset_in_resource   = offs64_hi;         899         offset_in_resource   = offs64_hi;
827         offset_in_resource <<= 32;                900         offset_in_resource <<= 32;
828         offset_in_resource  |= offs64_lo;         901         offset_in_resource  |= offs64_lo;
829                                                   902 
830         retval = sos_memcpy_from_user((sos_vad    903         retval = sos_memcpy_from_user((sos_vaddr_t)& hint_uaddr,
831                                       ptr_hint    904                                       ptr_hint_uaddr,
832                                       sizeof(h    905                                       sizeof(hint_uaddr));
833         if (sizeof(hint_uaddr) != retval)         906         if (sizeof(hint_uaddr) != retval)
834           {                                       907           {
835             retval = -SOS_EFAULT;                 908             retval = -SOS_EFAULT;
836             break;                                909             break;
837           }                                       910           }
838                                                   911 
839         retval = sos_fs_mmap(of, & hint_uaddr,    912         retval = sos_fs_mmap(of, & hint_uaddr, length, prot, flags,
840                              offset_in_resourc    913                              offset_in_resource);
841         if (SOS_OK == retval)                     914         if (SOS_OK == retval)
842           {                                       915           {
843             if (sizeof(hint_uaddr)                916             if (sizeof(hint_uaddr)
844                 != sos_memcpy_to_user(ptr_hint    917                 != sos_memcpy_to_user(ptr_hint_uaddr,
845                                       (sos_vad    918                                       (sos_vaddr_t)& hint_uaddr,
846                                       sizeof(h    919                                       sizeof(hint_uaddr)))
847               {                                   920               {
848                 sos_umem_vmm_unmap(sos_process    921                 sos_umem_vmm_unmap(sos_process_get_address_space(proc),
849                                    hint_uaddr,    922                                    hint_uaddr, length);
850                 retval = -SOS_EFAULT;             923                 retval = -SOS_EFAULT;
851               }                                   924               }
852           }                                       925           }
853                                                   926 
854       }                                           927       }
855       break;                                      928       break;
856                                                   929 
857     case SOS_SYSCALL_ID_FSYNC:                    930     case SOS_SYSCALL_ID_FSYNC:
858       {                                           931       {
859         struct sos_fs_opened_file * of;           932         struct sos_fs_opened_file * of;
860         struct sos_process * proc;                933         struct sos_process * proc;
861         int fd;                                   934         int fd;
862                                                   935 
863         proc = sos_thread_get_current()->proce    936         proc = sos_thread_get_current()->process;
864         retval = sos_syscall_get1arg(user_ctxt    937         retval = sos_syscall_get1arg(user_ctxt,
865                                      (unsigned    938                                      (unsigned int*)& fd);
866         if (SOS_OK != retval)                     939         if (SOS_OK != retval)
867           break;                                  940           break;
868                                                   941 
869         of = sos_process_get_opened_file(proc,    942         of = sos_process_get_opened_file(proc, fd);
870         if (NULL == of)                           943         if (NULL == of)
871           {                                       944           {
872             retval = -SOS_EBADF;                  945             retval = -SOS_EBADF;
873             break;                                946             break;
874           }                                       947           }
875                                                   948 
876         /* Do the actual sync */                  949         /* Do the actual sync */
877         retval = sos_fs_fsync(of);                950         retval = sos_fs_fsync(of);
878       }                                           951       }
879       break;                                      952       break;
880                                                   953 
881     case SOS_SYSCALL_ID_FCNTL:                    954     case SOS_SYSCALL_ID_FCNTL:
882       {                                           955       {
883         struct sos_fs_opened_file * of;           956         struct sos_fs_opened_file * of;
884         struct sos_process * proc;                957         struct sos_process * proc;
885         sos_ui32_t cmd, arg;                      958         sos_ui32_t cmd, arg;
886         int fd;                                   959         int fd;
887                                                   960 
888         proc = sos_thread_get_current()->proce    961         proc = sos_thread_get_current()->process;
889         retval = sos_syscall_get3args(user_ctx    962         retval = sos_syscall_get3args(user_ctxt,
890                                       (unsigne    963                                       (unsigned int*)& fd,
891                                       (unsigne    964                                       (unsigned int*)& cmd,
892                                       (unsigne    965                                       (unsigned int*)& arg);
893         if (SOS_OK != retval)                     966         if (SOS_OK != retval)
894           break;                                  967           break;
895                                                   968 
896         of = sos_process_get_opened_file(proc,    969         of = sos_process_get_opened_file(proc, fd);
897         if (NULL == of)                           970         if (NULL == of)
898           {                                       971           {
899             retval = -SOS_EBADF;                  972             retval = -SOS_EBADF;
900             break;                                973             break;
901           }                                       974           }
902                                                   975 
903         /* Do the actual fcntl */                 976         /* Do the actual fcntl */
904         retval = sos_fs_fcntl(of, cmd, arg);      977         retval = sos_fs_fcntl(of, cmd, arg);
905       }                                           978       }
906       break;                                      979       break;
907                                                   980 
908     case SOS_SYSCALL_ID_IOCTL:                 << 
909       {                                        << 
910         struct sos_fs_opened_file * of;        << 
911         struct sos_process * proc;             << 
912         sos_ui32_t cmd, arg;                   << 
913         int fd;                                << 
914                                                << 
915         proc = sos_thread_get_current()->proce << 
916         retval = sos_syscall_get3args(user_ctx << 
917                                       (unsigne << 
918                                       (unsigne << 
919                                       (unsigne << 
920         if (SOS_OK != retval)                  << 
921           break;                               << 
922                                                << 
923         of = sos_process_get_opened_file(proc, << 
924         if (NULL == of)                        << 
925           {                                    << 
926             retval = -SOS_EBADF;               << 
927             break;                             << 
928           }                                    << 
929                                                << 
930         /* Do the actual ioctl */              << 
931         retval = sos_fs_ioctl(of, cmd, arg);   << 
932       }                                        << 
933       break;                                   << 
934                                                << 
935     case SOS_SYSCALL_ID_CREAT:                    981     case SOS_SYSCALL_ID_CREAT:
936       {                                           982       {
937         sos_uaddr_t user_str;                     983         sos_uaddr_t user_str;
938         sos_size_t  len;                          984         sos_size_t  len;
939         sos_ui32_t  access_rights;                985         sos_ui32_t  access_rights;
940         char * path;                              986         char * path;
941         struct sos_process * proc;                987         struct sos_process * proc;
942                                                   988 
943         proc = sos_thread_get_current()->proce    989         proc = sos_thread_get_current()->process;
944         retval = sos_syscall_get3args(user_ctx    990         retval = sos_syscall_get3args(user_ctxt,
945                                       (unsigne    991                                       (unsigned int*)& user_str,
946                                       (unsigne    992                                       (unsigned int*)& len,
947                                       (unsigne    993                                       (unsigned int*)& access_rights);
948         if (SOS_OK != retval)                     994         if (SOS_OK != retval)
949           break;                                  995           break;
950                                                   996 
951         retval = sos_strndup_from_user(&path,  !! 997         retval = sos_memdup_from_user((sos_vaddr_t*) &path, user_str, len, 0);
952         if (SOS_OK != retval)                     998         if (SOS_OK != retval)
953           break;                                  999           break;
954                                                   1000 
955         retval = sos_fs_creat(proc,               1001         retval = sos_fs_creat(proc,
956                               path, len,          1002                               path, len,
957                               access_rights);     1003                               access_rights);
958         sos_kfree((sos_vaddr_t)path);             1004         sos_kfree((sos_vaddr_t)path);
959       }                                           1005       }
960       break;                                      1006       break;
961                                                   1007 
962     case SOS_SYSCALL_ID_LINK:                     1008     case SOS_SYSCALL_ID_LINK:
963     case SOS_SYSCALL_ID_RENAME:                   1009     case SOS_SYSCALL_ID_RENAME:
964       {                                           1010       {
965         sos_uaddr_t user_oldpath, user_newpath    1011         sos_uaddr_t user_oldpath, user_newpath;
966         sos_size_t  oldpathlen, newpathlen;       1012         sos_size_t  oldpathlen, newpathlen;
967         char * kernel_oldpath, * kernel_newpat    1013         char * kernel_oldpath, * kernel_newpath;
968         struct sos_process * proc;                1014         struct sos_process * proc;
969                                                   1015 
970         proc = sos_thread_get_current()->proce    1016         proc = sos_thread_get_current()->process;
971         retval = sos_syscall_get4args(user_ctx    1017         retval = sos_syscall_get4args(user_ctxt,
972                                       (unsigne    1018                                       (unsigned int*)& user_oldpath,
973                                       (unsigne    1019                                       (unsigned int*)& oldpathlen,
974                                       (unsigne    1020                                       (unsigned int*)& user_newpath,
975                                       (unsigne    1021                                       (unsigned int*)& newpathlen);
976         if (SOS_OK != retval)                     1022         if (SOS_OK != retval)
977           break;                                  1023           break;
978                                                   1024 
979         retval = sos_strndup_from_user(&kernel !! 1025         retval = sos_memdup_from_user((sos_vaddr_t*) &kernel_oldpath,
980                                        user_ol !! 1026                                       user_oldpath,
981                                        oldpath !! 1027                                       oldpathlen, 0);
982         if (SOS_OK != retval)                     1028         if (SOS_OK != retval)
983           break;                                  1029           break;
984                                                   1030 
985         retval = sos_strndup_from_user(&kernel !! 1031         retval = sos_memdup_from_user((sos_vaddr_t*) &kernel_newpath,
986                                        user_ne !! 1032                                       user_newpath,
987                                        newpath !! 1033                                       newpathlen, 0);
988         if (SOS_OK != retval)                     1034         if (SOS_OK != retval)
989           {                                       1035           {
990             sos_kfree((sos_vaddr_t) kernel_old    1036             sos_kfree((sos_vaddr_t) kernel_oldpath);
991             break;                                1037             break;
992           }                                       1038           }
993                                                   1039 
994         if (syscall_id == SOS_SYSCALL_ID_LINK)    1040         if (syscall_id == SOS_SYSCALL_ID_LINK)
995           retval = sos_fs_link(proc,              1041           retval = sos_fs_link(proc,
996                                kernel_oldpath,    1042                                kernel_oldpath, oldpathlen,
997                                kernel_newpath,    1043                                kernel_newpath, newpathlen);
998         else                                      1044         else
999           retval = sos_fs_rename(proc,            1045           retval = sos_fs_rename(proc,
1000                                  kernel_oldpa    1046                                  kernel_oldpath, oldpathlen,
1001                                  kernel_newpa    1047                                  kernel_newpath, newpathlen);
1002         sos_kfree((sos_vaddr_t)kernel_oldpath    1048         sos_kfree((sos_vaddr_t)kernel_oldpath);
1003         sos_kfree((sos_vaddr_t)kernel_newpath    1049         sos_kfree((sos_vaddr_t)kernel_newpath);
1004       }                                          1050       }
1005       break;                                     1051       break;
1006                                                  1052 
1007     case SOS_SYSCALL_ID_UNLINK:                  1053     case SOS_SYSCALL_ID_UNLINK:
1008       {                                          1054       {
1009         sos_uaddr_t user_str;                    1055         sos_uaddr_t user_str;
1010         sos_size_t  len;                         1056         sos_size_t  len;
1011         char * path;                             1057         char * path;
1012         struct sos_process * proc;               1058         struct sos_process * proc;
1013                                                  1059 
1014         proc = sos_thread_get_current()->proc    1060         proc = sos_thread_get_current()->process;
1015         retval = sos_syscall_get2args(user_ct    1061         retval = sos_syscall_get2args(user_ctxt,
1016                                       (unsign    1062                                       (unsigned int*)& user_str,
1017                                       (unsign    1063                                       (unsigned int*)& len);
1018         if (SOS_OK != retval)                    1064         if (SOS_OK != retval)
1019           break;                                 1065           break;
1020                                                  1066 
1021         retval = sos_strndup_from_user(&path, !! 1067         retval = sos_memdup_from_user((sos_vaddr_t*) &path, user_str, len, 0);
1022         if (SOS_OK != retval)                    1068         if (SOS_OK != retval)
1023           break;                                 1069           break;
1024                                                  1070 
1025         retval = sos_fs_unlink(proc,             1071         retval = sos_fs_unlink(proc,
1026                                path, len);       1072                                path, len);
1027         sos_kfree((sos_vaddr_t)path);            1073         sos_kfree((sos_vaddr_t)path);
1028       }                                          1074       }
1029       break;                                     1075       break;
1030                                                  1076 
1031     case SOS_SYSCALL_ID_SYMLINK:                 1077     case SOS_SYSCALL_ID_SYMLINK:
1032       {                                          1078       {
1033         sos_uaddr_t user_path, user_targetpat    1079         sos_uaddr_t user_path, user_targetpath;
1034         sos_size_t  pathlen, targetpathlen;      1080         sos_size_t  pathlen, targetpathlen;
1035         char * kernel_path;                      1081         char * kernel_path;
1036         struct sos_process * proc;               1082         struct sos_process * proc;
1037                                                  1083 
1038         proc = sos_thread_get_current()->proc    1084         proc = sos_thread_get_current()->process;
1039         retval = sos_syscall_get4args(user_ct    1085         retval = sos_syscall_get4args(user_ctxt,
1040                                       (unsign    1086                                       (unsigned int*)& user_path,
1041                                       (unsign    1087                                       (unsigned int*)& pathlen,
1042                                       (unsign    1088                                       (unsigned int*)& user_targetpath,
1043                                       (unsign    1089                                       (unsigned int*)& targetpathlen);
1044         if (SOS_OK != retval)                    1090         if (SOS_OK != retval)
1045           break;                                 1091           break;
1046                                                  1092 
1047         retval = sos_strndup_from_user(&kerne !! 1093         retval = sos_memdup_from_user((sos_vaddr_t*) &kernel_path,
1048                                        user_p !! 1094                                       user_path,
1049                                        pathle !! 1095                                       pathlen, 0);
1050         if (SOS_OK != retval)                    1096         if (SOS_OK != retval)
1051           break;                                 1097           break;
1052                                                  1098 
1053         retval = sos_fs_symlink(proc,            1099         retval = sos_fs_symlink(proc,
1054                                 kernel_path,     1100                                 kernel_path, pathlen,
1055                                 user_targetpa    1101                                 user_targetpath, targetpathlen);
1056         sos_kfree((sos_vaddr_t)kernel_path);     1102         sos_kfree((sos_vaddr_t)kernel_path);
1057       }                                          1103       }
1058       break;                                     1104       break;
1059                                                  1105 
1060     case SOS_SYSCALL_ID_MKNOD:                << 
1061       {                                       << 
1062         sos_uaddr_t user_str;                 << 
1063         sos_size_t  len;                      << 
1064         sos_ui32_t  access_rights;            << 
1065         int type;                             << 
1066         char * path;                          << 
1067         struct sos_fs_dev_id_t dev_id;        << 
1068         struct sos_process * proc;            << 
1069                                               << 
1070         proc = sos_thread_get_current()->proc << 
1071         retval = sos_syscall_get6args(user_ct << 
1072                                       (unsign << 
1073                                       (unsign << 
1074                                       (unsign << 
1075                                       (unsign << 
1076                                       (unsign << 
1077                                       (unsign << 
1078         if (SOS_OK != retval)                 << 
1079           break;                              << 
1080                                               << 
1081         retval = sos_strndup_from_user(&path, << 
1082         if (SOS_OK != retval)                 << 
1083           break;                              << 
1084                                               << 
1085         switch (type)                         << 
1086           {                                   << 
1087           case SOS_FS_NODE_REGULAR_FILE:      << 
1088             retval = sos_fs_creat(proc, path, << 
1089             break;                            << 
1090                                               << 
1091           case SOS_FS_NODE_DIRECTORY:         << 
1092             retval = sos_fs_mkdir(proc, path, << 
1093             break;                            << 
1094                                               << 
1095           case SOS_FS_NODE_SYMLINK:           << 
1096             retval = -SOS_ENOSUP;             << 
1097             break;                            << 
1098                                               << 
1099           case SOS_FS_NODE_DEVICE_CHAR:       << 
1100             retval = sos_fs_mknod(proc,       << 
1101                                   path, len,  << 
1102             break;                            << 
1103                                               << 
1104           default:                            << 
1105             retval = -SOS_EINVAL;             << 
1106             break;                            << 
1107           }                                   << 
1108                                               << 
1109         sos_kfree((sos_vaddr_t)path);         << 
1110       }                                       << 
1111       break;                                  << 
1112                                               << 
1113     case SOS_SYSCALL_ID_MKDIR:                   1106     case SOS_SYSCALL_ID_MKDIR:
1114       {                                          1107       {
1115         sos_uaddr_t user_str;                    1108         sos_uaddr_t user_str;
1116         sos_size_t  len;                         1109         sos_size_t  len;
1117         sos_ui32_t  access_rights;               1110         sos_ui32_t  access_rights;
1118         char * path;                             1111         char * path;
1119         struct sos_process * proc;               1112         struct sos_process * proc;
1120                                                  1113 
1121         proc = sos_thread_get_current()->proc    1114         proc = sos_thread_get_current()->process;
1122         retval = sos_syscall_get3args(user_ct    1115         retval = sos_syscall_get3args(user_ctxt,
1123                                       (unsign    1116                                       (unsigned int*)& user_str,
1124                                       (unsign    1117                                       (unsigned int*)& len,
1125                                       (unsign    1118                                       (unsigned int*)& access_rights);
1126         if (SOS_OK != retval)                    1119         if (SOS_OK != retval)
1127           break;                                 1120           break;
1128                                                  1121 
1129         retval = sos_strndup_from_user(&path, !! 1122         retval = sos_memdup_from_user((sos_vaddr_t*) &path, user_str, len, 0);
1130         if (SOS_OK != retval)                    1123         if (SOS_OK != retval)
1131           break;                                 1124           break;
1132                                                  1125 
1133         retval = sos_fs_mkdir(proc,              1126         retval = sos_fs_mkdir(proc,
1134                               path, len, acce    1127                               path, len, access_rights);
1135         sos_kfree((sos_vaddr_t)path);            1128         sos_kfree((sos_vaddr_t)path);
1136       }                                          1129       }
1137       break;                                     1130       break;
1138                                                  1131 
1139     case SOS_SYSCALL_ID_RMDIR:                   1132     case SOS_SYSCALL_ID_RMDIR:
1140       {                                          1133       {
1141         sos_uaddr_t user_str;                    1134         sos_uaddr_t user_str;
1142         sos_size_t  len;                         1135         sos_size_t  len;
1143         char * path;                             1136         char * path;
1144         struct sos_process * proc;               1137         struct sos_process * proc;
1145                                                  1138 
1146         proc = sos_thread_get_current()->proc    1139         proc = sos_thread_get_current()->process;
1147         retval = sos_syscall_get2args(user_ct    1140         retval = sos_syscall_get2args(user_ctxt,
1148                                       (unsign    1141                                       (unsigned int*)& user_str,
1149                                       (unsign    1142                                       (unsigned int*)& len);
1150         if (SOS_OK != retval)                    1143         if (SOS_OK != retval)
1151           break;                                 1144           break;
1152                                                  1145 
1153         retval = sos_strndup_from_user(&path, !! 1146         retval = sos_memdup_from_user((sos_vaddr_t*) &path, user_str, len, 0);
1154         if (SOS_OK != retval)                    1147         if (SOS_OK != retval)
1155           break;                                 1148           break;
1156                                                  1149 
1157         retval = sos_fs_rmdir(proc, path, len    1150         retval = sos_fs_rmdir(proc, path, len);
1158         sos_kfree((sos_vaddr_t)path);            1151         sos_kfree((sos_vaddr_t)path);
1159       }                                          1152       }
1160       break;                                     1153       break;
1161                                                  1154 
1162     case SOS_SYSCALL_ID_CHMOD:                   1155     case SOS_SYSCALL_ID_CHMOD:
1163       {                                          1156       {
1164         sos_uaddr_t user_str;                    1157         sos_uaddr_t user_str;
1165         sos_size_t  len;                         1158         sos_size_t  len;
1166         sos_ui32_t  access_rights;               1159         sos_ui32_t  access_rights;
1167         char * path;                             1160         char * path;
1168         struct sos_process * proc;               1161         struct sos_process * proc;
1169                                                  1162 
1170         proc = sos_thread_get_current()->proc    1163         proc = sos_thread_get_current()->process;
1171         retval = sos_syscall_get3args(user_ct    1164         retval = sos_syscall_get3args(user_ctxt,
1172                                       (unsign    1165                                       (unsigned int*)& user_str,
1173                                       (unsign    1166                                       (unsigned int*)& len,
1174                                       (unsign    1167                                       (unsigned int*)& access_rights);
1175         if (SOS_OK != retval)                    1168         if (SOS_OK != retval)
1176           break;                                 1169           break;
1177                                                  1170 
1178         retval = sos_strndup_from_user(&path, !! 1171         retval = sos_memdup_from_user((sos_vaddr_t*) &path, user_str, len, 0);
1179         if (SOS_OK != retval)                    1172         if (SOS_OK != retval)
1180           break;                                 1173           break;
1181                                                  1174 
1182         retval = sos_fs_chmod(proc, path, len    1175         retval = sos_fs_chmod(proc, path, len, access_rights);
1183         sos_kfree((sos_vaddr_t)path);            1176         sos_kfree((sos_vaddr_t)path);
1184       }                                          1177       }
1185       break;                                     1178       break;
1186                                                  1179 
1187     case SOS_SYSCALL_ID_STAT64:                  1180     case SOS_SYSCALL_ID_STAT64:
1188       {                                          1181       {
1189         sos_uaddr_t user_str;                    1182         sos_uaddr_t user_str;
1190         sos_size_t  len;                         1183         sos_size_t  len;
1191         sos_uaddr_t user_stat_struct;            1184         sos_uaddr_t user_stat_struct;
1192         struct sos_fs_stat kernel_stat_struct    1185         struct sos_fs_stat kernel_stat_struct;
1193         int nofollow;                            1186         int nofollow;
1194         char * path;                             1187         char * path;
1195         struct sos_process * proc;               1188         struct sos_process * proc;
1196                                                  1189 
1197         proc = sos_thread_get_current()->proc    1190         proc = sos_thread_get_current()->process;
1198         retval = sos_syscall_get4args(user_ct    1191         retval = sos_syscall_get4args(user_ctxt,
1199                                       (unsign    1192                                       (unsigned int*)& user_str,
1200                                       (unsign    1193                                       (unsigned int*)& len,
1201                                       (unsign    1194                                       (unsigned int*)& nofollow,
1202                                       (unsign    1195                                       (unsigned int*)& user_stat_struct);
1203         if (SOS_OK != retval)                    1196         if (SOS_OK != retval)
1204           break;                                 1197           break;
1205                                                  1198 
1206         retval = sos_strndup_from_user(&path, !! 1199         retval = sos_memdup_from_user((sos_vaddr_t*) &path, user_str, len, 0);
1207         if (SOS_OK != retval)                    1200         if (SOS_OK != retval)
1208           break;                                 1201           break;
1209                                                  1202 
1210         retval = sos_fs_stat(proc, path, len,    1203         retval = sos_fs_stat(proc, path, len, nofollow, & kernel_stat_struct);
1211         sos_kfree((sos_vaddr_t)path);            1204         sos_kfree((sos_vaddr_t)path);
1212         if (SOS_OK != retval)                    1205         if (SOS_OK != retval)
1213           break;                                 1206           break;
1214                                                  1207 
1215         if (sizeof(kernel_stat_struct)           1208         if (sizeof(kernel_stat_struct)
1216             != sos_memcpy_to_user(user_stat_s    1209             != sos_memcpy_to_user(user_stat_struct,
1217                                   (sos_vaddr_    1210                                   (sos_vaddr_t) & kernel_stat_struct,
1218                                   sizeof(kern    1211                                   sizeof(kernel_stat_struct)))
1219           retval = -SOS_EFAULT;                  1212           retval = -SOS_EFAULT;
1220       }                                          1213       }
1221       break;                                     1214       break;
1222                                                  1215 
1223     case SOS_SYSCALL_ID_CHROOT:                  1216     case SOS_SYSCALL_ID_CHROOT:
1224     case SOS_SYSCALL_ID_CHDIR:                   1217     case SOS_SYSCALL_ID_CHDIR:
1225       {                                          1218       {
1226         sos_uaddr_t user_str;                    1219         sos_uaddr_t user_str;
1227         sos_size_t  len;                         1220         sos_size_t  len;
1228         char * path;                             1221         char * path;
1229         struct sos_fs_opened_file * of, * old    1222         struct sos_fs_opened_file * of, * old_of;
1230         struct sos_process * proc;               1223         struct sos_process * proc;
1231                                                  1224 
1232         proc = sos_thread_get_current()->proc    1225         proc = sos_thread_get_current()->process;
1233         retval = sos_syscall_get2args(user_ct    1226         retval = sos_syscall_get2args(user_ctxt,
1234                                       (unsign    1227                                       (unsigned int*)& user_str,
1235                                       (unsign    1228                                       (unsigned int*)& len);
1236         if (SOS_OK != retval)                    1229         if (SOS_OK != retval)
1237           break;                                 1230           break;
1238                                                  1231 
1239         retval = sos_strndup_from_user(&path, !! 1232         retval = sos_memdup_from_user((sos_vaddr_t*) &path, user_str, len, 0);
1240         if (SOS_OK != retval)                    1233         if (SOS_OK != retval)
1241           break;                                 1234           break;
1242                                                  1235 
1243         retval = sos_fs_open(proc,               1236         retval = sos_fs_open(proc,
1244                              path, len,          1237                              path, len,
1245                              SOS_FS_OPEN_DIRE    1238                              SOS_FS_OPEN_DIRECTORY,
1246                              SOS_FS_OPEN_READ    1239                              SOS_FS_OPEN_READ,
1247                              & of);              1240                              & of);
1248         sos_kfree((sos_vaddr_t)path);            1241         sos_kfree((sos_vaddr_t)path);
1249         if (SOS_OK != retval)                    1242         if (SOS_OK != retval)
1250           break;                                 1243           break;
1251                                                  1244 
1252         if (syscall_id == SOS_SYSCALL_ID_CHRO    1245         if (syscall_id == SOS_SYSCALL_ID_CHROOT)
1253           retval = sos_process_chroot(proc, o    1246           retval = sos_process_chroot(proc, of, & old_of);
1254         else                                     1247         else
1255           retval = sos_process_chdir(proc, of    1248           retval = sos_process_chdir(proc, of, & old_of);
1256                                                  1249 
1257         if (retval < 0)                          1250         if (retval < 0)
1258           {                                      1251           {
1259             sos_fs_close(of);                    1252             sos_fs_close(of);
1260             break;                               1253             break;
1261           }                                      1254           }
1262                                                  1255 
1263         sos_fs_close(old_of);                    1256         sos_fs_close(old_of);
1264       }                                          1257       }
1265       break;                                     1258       break;      
1266                                                  1259 
1267     case SOS_SYSCALL_ID_FCHDIR:                  1260     case SOS_SYSCALL_ID_FCHDIR:
1268       {                                          1261       {
1269         struct sos_fs_opened_file * of, * new    1262         struct sos_fs_opened_file * of, * new_of, * old_of;
1270         struct sos_process * proc;               1263         struct sos_process * proc;
1271         int fd;                                  1264         int fd;
1272                                                  1265 
1273         proc = sos_thread_get_current()->proc    1266         proc = sos_thread_get_current()->process;
1274         retval = sos_syscall_get1arg(user_ctx    1267         retval = sos_syscall_get1arg(user_ctxt,
1275                                      (unsigne    1268                                      (unsigned int*)& fd);
1276         if (SOS_OK != retval)                    1269         if (SOS_OK != retval)
1277           break;                                 1270           break;
1278                                                  1271 
1279         of = sos_process_get_opened_file(proc    1272         of = sos_process_get_opened_file(proc, fd);
1280         if (NULL == of)                          1273         if (NULL == of)
1281           {                                      1274           {
1282             retval = -SOS_EBADF;                 1275             retval = -SOS_EBADF;
1283             break;                               1276             break;
1284           }                                      1277           }
1285                                                  1278 
1286         /* Duplicate this FD */                  1279         /* Duplicate this FD */
1287         retval = sos_fs_duplicate_opened_file    1280         retval = sos_fs_duplicate_opened_file(of, proc, & new_of);
1288         if (SOS_OK != retval)                    1281         if (SOS_OK != retval)
1289           break;                                 1282           break;
1290                                                  1283 
1291         /* Do the actual chdir */                1284         /* Do the actual chdir */
1292         retval = sos_process_chdir(proc, new_    1285         retval = sos_process_chdir(proc, new_of, & old_of);
1293         if (retval < 0)                          1286         if (retval < 0)
1294           {                                      1287           {
1295             sos_fs_close(new_of);                1288             sos_fs_close(new_of);
1296             break;                               1289             break;
1297           }                                      1290           }
1298                                                  1291 
1299         sos_fs_close(old_of);                    1292         sos_fs_close(old_of);
1300       }                                          1293       }
1301       break;                                     1294       break;
1302                                                  1295 
1303     case SOS_SYSCALL_ID_BOCHS_WRITE:             1296     case SOS_SYSCALL_ID_BOCHS_WRITE:
1304       {                                          1297       {
1305         sos_uaddr_t user_str;                    1298         sos_uaddr_t user_str;
1306         sos_size_t len;                          1299         sos_size_t len;
1307         char * str;                              1300         char * str;
1308         retval = sos_syscall_get2args(user_ct    1301         retval = sos_syscall_get2args(user_ctxt, & user_str, & len);
1309         if (SOS_OK != retval)                    1302         if (SOS_OK != retval)
1310           break;                                 1303           break;
1311                                                  1304 
1312         retval = sos_strndup_from_user(& str,    1305         retval = sos_strndup_from_user(& str, user_str, len + 1, 0);
1313         if (SOS_OK == retval)                    1306         if (SOS_OK == retval)
1314           {                                      1307           {
1315             sos_bochs_printf("THR 0x%x: ",       1308             sos_bochs_printf("THR 0x%x: ",
1316                              (unsigned)sos_th    1309                              (unsigned)sos_thread_get_current());
1317             retval = sos_bochs_putstring(str)    1310             retval = sos_bochs_putstring(str);
1318             retval = len;                        1311             retval = len;
1319             sos_kfree((sos_vaddr_t)str);         1312             sos_kfree((sos_vaddr_t)str);
1320           }                                      1313           }
1321       }                                          1314       }
1322       break;                                     1315       break;
1323                                                  1316 
1324                                                  1317 
1325       /* ************************************    1318       /* ***********************************************
1326        * Debug syscalls (will be removed in t    1319        * Debug syscalls (will be removed in the future)
1327        */                                        1320        */
1328                                                  1321 
1329                                                  1322 
1330       /**                                        1323       /**
1331        * Syscall 4012: hexdump of a user-spac    1324        * Syscall 4012: hexdump of a user-space memory region
1332        * args: addr_start size, retval=ignore    1325        * args: addr_start size, retval=ignored
1333        */                                        1326        */
1334     case 4012:                                   1327     case 4012:
1335       {                                          1328       {
1336         sos_uaddr_t user_str;                    1329         sos_uaddr_t user_str;
1337         unsigned int len;                        1330         unsigned int len;
1338         unsigned char * str;                     1331         unsigned char * str;
1339                                                  1332 
1340         retval = sos_syscall_get2args(user_ct    1333         retval = sos_syscall_get2args(user_ctxt, & user_str, & len);
1341         if (SOS_OK != retval)                    1334         if (SOS_OK != retval)
1342           break;                                 1335           break;
1343                                                  1336 
1344         str = (unsigned char*)sos_kmalloc(len !! 1337         str = (char*)sos_kmalloc(len, 0);
1345         if (str)                                 1338         if (str)
1346           {                                      1339           {
1347             int i;                               1340             int i;
1348             sos_bochs_printf("Hexdump(0x%x, %    1341             sos_bochs_printf("Hexdump(0x%x, %d):\n", user_str, len);
1349             retval = sos_memcpy_from_user((so    1342             retval = sos_memcpy_from_user((sos_vaddr_t) str, user_str, len);
1350             sos_bochs_printf("  (Successfully    1343             sos_bochs_printf("  (Successfully copied %d out of %d)\n",
1351                              retval, len);       1344                              retval, len);
1352                                                  1345 
1353             for (i = 0 ; i < retval ; i++)       1346             for (i = 0 ; i < retval ; i++)
1354               {                                  1347               {
1355                 if ((i % 32) == 0)               1348                 if ((i % 32) == 0)
1356                   sos_bochs_printf("%x:", i);    1349                   sos_bochs_printf("%x:", i);
1357                 sos_bochs_printf(" %x", str[i    1350                 sos_bochs_printf(" %x", str[i]);
1358                 if (((i+1) % 32) == 0)           1351                 if (((i+1) % 32) == 0)
1359                   sos_bochs_printf("\n");        1352                   sos_bochs_printf("\n");
1360               }                                  1353               }
1361             if (i % 32)                          1354             if (i % 32)
1362               sos_bochs_printf("\n");            1355               sos_bochs_printf("\n");
1363                                                  1356 
1364             sos_kfree((sos_vaddr_t)str);         1357             sos_kfree((sos_vaddr_t)str);
1365           }                                      1358           }
1366         else                                     1359         else
1367           retval = -SOS_ENOMEM;                  1360           retval = -SOS_ENOMEM;
1368       }                                          1361       }
1369       break;                                     1362       break;
1370                                                  1363 
1371                                                  1364       
1372       /**                                        1365       /**
1373        * Syscall 4004: lists the VR of the cu    1366        * Syscall 4004: lists the VR of the current thread's address space
1374        * args: debug_string, retval=ignored      1367        * args: debug_string, retval=ignored
1375        */                                        1368        */
1376     case 4004:                                   1369     case 4004:
1377       {                                          1370       {
1378         sos_uaddr_t ustr;                        1371         sos_uaddr_t ustr;
1379         char * kstr;                             1372         char * kstr;
1380         struct sos_umem_vmm_as * my_as;          1373         struct sos_umem_vmm_as * my_as;
1381                                                  1374 
1382         retval = sos_syscall_get1arg(user_ctx    1375         retval = sos_syscall_get1arg(user_ctxt, & ustr);
1383         if (SOS_OK != retval)                    1376         if (SOS_OK != retval)
1384           break;                                 1377           break;
1385                                                  1378 
1386         retval = sos_strndup_from_user(& kstr    1379         retval = sos_strndup_from_user(& kstr, ustr, 256, 0);
1387         if (SOS_OK != retval)                    1380         if (SOS_OK != retval)
1388           break;                                 1381           break;
1389                                                  1382 
1390         extern void sos_dump_as(const struct     1383         extern void sos_dump_as(const struct sos_umem_vmm_as *, const char *);
1391         my_as                                    1384         my_as
1392           = sos_process_get_address_space(sos    1385           = sos_process_get_address_space(sos_thread_get_current()->process);
1393         sos_dump_as(my_as, kstr);                1386         sos_dump_as(my_as, kstr);
1394         sos_kfree((sos_vaddr_t)kstr);            1387         sos_kfree((sos_vaddr_t)kstr);
1395       }                                          1388       }
1396       break;                                     1389       break;
1397                                                  1390 
1398                                                  1391 
1399       /**                                        1392       /**
1400        * Syscall 4008: dump the list of proce    1393        * Syscall 4008: dump the list of processes in the system
1401        * args: none, retval=ignored              1394        * args: none, retval=ignored
1402        */                                        1395        */
1403     case 4008:                                   1396     case 4008:
1404       {                                          1397       {
1405         extern void sos_process_dumplist(void    1398         extern void sos_process_dumplist(void);
1406         sos_process_dumplist();                  1399         sos_process_dumplist();
1407         retval = SOS_OK;                         1400         retval = SOS_OK;
1408       }                                          1401       }
1409       break;                                     1402       break;
1410                                                  1403 
1411     default:                                     1404     default:
1412       sos_bochs_printf("Syscall: UNKNOWN[%d]\    1405       sos_bochs_printf("Syscall: UNKNOWN[%d]\n", syscall_id);
1413       retval = -SOS_ENOSUP;                      1406       retval = -SOS_ENOSUP;
1414     }                                            1407     }
1415                                                  1408   
1416   return retval;                                 1409   return retval;
1417 }                                                1410 }
                                                      

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