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


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

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