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 ]

001 /* Copyright (C) 2005 David Decotigny
002    Copyright (C) 2003 Thomas Petazzoni
003 
004    This program is free software; you can redistribute it and/or
005    modify it under the terms of the GNU General Public License
006    as published by the Free Software Foundation; either version 2
007    of the License, or (at your option) any later version.
008    
009    This program is distributed in the hope that it will be useful,
010    but WITHOUT ANY WARRANTY; without even the implied warranty of
011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012    GNU General Public License for more details.
013    
014    You should have received a copy of the GNU General Public License
015    along with this program; if not, write to the Free Software
016    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
017    USA. 
018 */
019 
020 
021 /**
022  * @file crt.c
023  *
024  * The C RunTime environment for the basic support of SOS C user
025  * programs
026  */
027 
028 #include <hwcore/swintr.h>
029 #include <string.h>
030 #include "crt.h"
031 
032 /**
033  * Starter function !
034  */
035 void _start() __attribute__((noreturn));
036 void _start()
037 {
038   /* This starter function expects a main() function somewhere */
039   extern int main();
040 
041   /* Reset the bss section */
042   extern char _bbss, _ebss;
043   memset(& _bbss, 0x0, (& _ebss) - (& _bbss));
044 
045   _sos_exit(main());
046 }
047 
048 
049 /*
050  * By convention, the USER SOS programs always pass 4 arguments to the
051  * kernel syscall handler: in eax/../edx. For less arguments, the
052  * unused registers are filled with 0s. For more arguments, the 4th
053  * syscall parameter gives the address of the array containing the
054  * remaining arguments. In any case, eax corresponds to the syscall
055  * IDentifier.
056  */
057 
058 
059 inline
060 int _sos_syscall3(int id,
061                   unsigned int arg1,
062                   unsigned int arg2,
063                   unsigned int arg3)
064 {
065   int ret;
066   
067   asm volatile("movl %1,%%eax \n"
068                "movl %2,%%ebx \n"
069                "movl %3,%%ecx \n"
070                "movl %4,%%edx \n"
071                "int  %5\n"
072                "movl %%eax, %0"
073                :"=g"(ret)
074                :"g"(id),"g"(arg1),"g"(arg2),"g"(arg3)
075                 ,"i"(SOS_SWINTR_SOS_SYSCALL)
076                :"eax","ebx","ecx","edx","memory");
077 
078   return ret;
079 }
080 
081 
082 int _sos_syscall0(int id)
083 {
084   return _sos_syscall3(id, 0, 0, 0);
085 }
086 
087 
088 int _sos_syscall1(int id,
089              unsigned int arg1)
090 {
091   return _sos_syscall3(id, arg1, 0, 0);
092 }
093 
094 
095 int _sos_syscall2(int id,
096                   unsigned int arg1,
097                   unsigned int arg2)
098 {
099   return _sos_syscall3(id, arg1, arg2, 0);
100 }
101 
102 
103 int _sos_syscall4(int id,
104                   unsigned int arg1,
105                   unsigned int arg2,
106                   unsigned int arg3,
107                   unsigned int arg4)
108 {
109   unsigned int args[] = { arg3, arg4 };
110   return _sos_syscall3(id, arg1, arg2, (unsigned)args);
111 }
112 
113 
114 int _sos_syscall5(int id,
115                   unsigned int arg1,
116                   unsigned int arg2,
117                   unsigned int arg3,
118                   unsigned int arg4,
119                   unsigned int arg5)
120 {
121   unsigned int args[] = { arg3, arg4, arg5 };
122   return _sos_syscall3(id, arg1, arg2, (unsigned)args);
123 }
124 
125 
126 int _sos_syscall6(int id,
127                   unsigned int arg1,
128                   unsigned int arg2,
129                   unsigned int arg3,
130                   unsigned int arg4,
131                   unsigned int arg5,
132                   unsigned int arg6)
133 {
134   unsigned int args[] = { arg3, arg4, arg5, arg6 };
135   return _sos_syscall3(id, arg1, arg2, (unsigned)args);
136 }
137 
138 
139 int _sos_syscall7(int id,
140                   unsigned int arg1,
141                   unsigned int arg2,
142                   unsigned int arg3,
143                   unsigned int arg4,
144                   unsigned int arg5,
145                   unsigned int arg6,
146                   unsigned int arg7)
147 {
148   unsigned int args[] = { arg3, arg4, arg5, arg6, arg7 };
149   return _sos_syscall3(id, arg1, arg2, (unsigned)args);
150 }
151 
152 
153 int _sos_syscall8(int id,
154                   unsigned int arg1,
155                   unsigned int arg2,
156                   unsigned int arg3,
157                   unsigned int arg4,
158                   unsigned int arg5,
159                   unsigned int arg6,
160                   unsigned int arg7,
161                   unsigned int arg8)
162 {
163   unsigned int args[] = { arg3, arg4, arg5, arg6, arg7, arg8 };
164   return _sos_syscall3(id, arg1, arg2, (unsigned)args);
165 }
166 
167 
168 void _sos_exit(int status)
169 {
170   _sos_syscall1(SOS_SYSCALL_ID_EXIT, (unsigned)status);
171   
172   /* Never reached ! */
173   for ( ; ; )
174     ;
175 }
176 
177 
178 int _sos_bochs_write(const char * str, unsigned length)
179 {
180   return _sos_syscall2(SOS_SYSCALL_ID_BOCHS_WRITE,
181                        (unsigned)str,
182                        length);
183 }
184 
185 
186 int _sos_fork()
187 {
188   return _sos_syscall0(SOS_SYSCALL_ID_FORK);
189 }
190 
191 
192 int _sos_exec(const char * prog)
193 {
194   return _sos_syscall2(SOS_SYSCALL_ID_EXEC, (unsigned int)prog,
195                        (unsigned int)strlen(prog));
196 }
197 
198 
199 int _sos_munmap(void * start, size_t length)
200 {
201   return _sos_syscall2(SOS_SYSCALL_ID_MUNMAP,
202                        (unsigned int)start,
203                        length);
204 }
205 
206 
207 int _sos_mprotect(const void *addr, size_t len, int prot)
208 {
209   return _sos_syscall3(SOS_SYSCALL_ID_MPROTECT,
210                        (unsigned int)addr,
211                        len,
212                        (unsigned int)prot);
213 }
214 
215 
216 int _sos_mresize(void * old_addr, size_t old_len,
217                  void * *new_addr, size_t new_len,
218                  unsigned long flags)
219 {
220   return _sos_syscall5(SOS_SYSCALL_ID_MRESIZE,
221                        (unsigned int)old_addr,
222                        old_len,
223                        (unsigned int)new_addr,
224                        new_len,
225                        flags);
226 }
227 
228 
229 /**
230  * Helper function that represents the start routine of a new user
231  * thread created from user space (syscall new_thread). It takes 2
232  * arguments that are passsed in the eax/ebx registers (@see
233  * cpu_ustate_init() in hwcore/cpu_context.c): the start function of
234  * the new thread (eax), the argument passed to it (ebx).
235  */
236 static void thread_routine()
237 {
238   /* NOTE: variables as named registers is a gcc extension */
239   register unsigned long int reg_arg1 asm("%eax");
240   register unsigned long int reg_arg2 asm("%ebx");
241 
242   sos_thread_func_t * func = (sos_thread_func_t*)reg_arg1;
243   unsigned long int arg = reg_arg2;
244 
245   func(arg);
246   _sos_exit(0);
247 }
248 
249 
250 int _sos_new_thread(sos_thread_func_t *func,
251                     void* arg,
252                     size_t stack_size)
253 {
254   return _sos_syscall4(SOS_SYSCALL_ID_NEW_THREAD,
255                        (unsigned)thread_routine,
256                        (unsigned)func, (unsigned)arg,
257                        stack_size);
258 }
259 
260 
261 int _sos_nanosleep(unsigned long int sec,
262                    unsigned long int nanosec)
263 {
264   return _sos_syscall2(SOS_SYSCALL_ID_NANOSLEEP,
265                        sec, nanosec);
266 }
267 
268 
269 void * _sos_brk(void * new_top_address)
270 {
271   return (void*)_sos_syscall1(SOS_SYSCALL_ID_BRK,
272                               (unsigned)new_top_address);
273 }
274 
275 
276 int _sos_mount(const char *source, const char *target,
277                const char *filesystemtype, unsigned long mountflags,
278                const char *args)
279 {
280   if (!target || !filesystemtype)
281     return -1;
282 
283   return _sos_syscall7(SOS_SYSCALL_ID_MOUNT,
284                        (unsigned int)source, source?strlen(source):0,
285                        (unsigned int)target, strlen(target),
286                        (unsigned int)filesystemtype,
287                        mountflags,
288                        (unsigned int)args);
289 }
290 
291 
292 int _sos_umount(const char *target)
293 {
294   if (!target)
295     return -1;
296 
297   return _sos_syscall2(SOS_SYSCALL_ID_UMOUNT,
298                        (unsigned int)target,
299                        strlen(target));
300 }
301 
302 
303 void _sos_sync(void)
304 {
305   _sos_syscall0(SOS_SYSCALL_ID_SYNC);
306 }
307 
308 
309 int _sos_statvfs(const char *path, struct statvfs *buf)
310 {
311   if (! path)
312     return -1;
313     
314   return _sos_syscall3(SOS_SYSCALL_ID_VFSTAT64,
315                        (unsigned)path,
316                        strlen(path),
317                        (unsigned)buf);
318 }
319 
320 
321 int _sos_open(const char * pathname, int flags, int mode)
322 {
323   if (! pathname)
324     return -1;
325 
326   return _sos_syscall4(SOS_SYSCALL_ID_OPEN,
327                        (unsigned)pathname,
328                        strlen(pathname),
329                        flags,
330                        mode);
331 }
332 
333 
334 int _sos_close(int fd)
335 {
336   return _sos_syscall1(SOS_SYSCALL_ID_CLOSE, fd);
337 }
338 
339 
340 int _sos_read(int fd, char * buf, size_t * len)
341 {
342   return _sos_syscall3(SOS_SYSCALL_ID_READ, fd,
343                        (unsigned int) buf,
344                        (unsigned int) len);
345 }
346 
347 
348 int _sos_write(int fd, const char * buf, size_t * len)
349 {
350   return _sos_syscall3(SOS_SYSCALL_ID_WRITE, fd,
351                        (unsigned int) buf,
352                        (unsigned int) len);
353 }
354 
355 
356 int _sos_seek64(int fd, loff_t * offset, int whence)
357 {
358   return _sos_syscall3(SOS_SYSCALL_ID_SEEK64, fd,
359                        (unsigned int)offset,
360                        (unsigned int)whence);
361 }
362 
363 
364 int _sos_fmmap(void ** ptr_hint_addr, size_t len, int prot, int flags,
365                int fd, loff_t offset)
366 {
367   return _sos_syscall7(SOS_SYSCALL_ID_FSMMAP,
368                        (unsigned int)ptr_hint_addr, len, prot, flags,
369                        (unsigned int)fd,
370                        /* offs64_hi */(offset >> 32),
371                        /* offs64_lo */(offset & 0xffffffff));
372 }
373 
374 
375 int _sos_ftruncate64(int fd, loff_t length)
376 {
377   return _sos_syscall2(SOS_SYSCALL_ID_FTRUNCATE64, fd,
378                        (unsigned int)length);  
379 }
380 
381 
382 int _sos_fcntl(int fd, int cmd, int arg)
383 {
384   return _sos_syscall3(SOS_SYSCALL_ID_FCNTL, fd,
385                        (unsigned int)cmd,
386                        (unsigned int)arg);
387 }
388 
389 
390 int _sos_ioctl(int fd, int cmd, int arg)
391 {
392   return _sos_syscall3(SOS_SYSCALL_ID_IOCTL, fd,
393                        (unsigned int)cmd,
394                        (unsigned int)arg);
395 }
396 
397 
398 int _sos_creat(const char *pathname, int mode)
399 {
400   if (! pathname)
401     return -1;
402 
403   return _sos_syscall3(SOS_SYSCALL_ID_CREAT,
404                        (unsigned int)pathname,
405                        strlen(pathname),
406                        mode);
407 }
408 
409 
410 int _sos_link (const char *oldpath, const char *newpath)
411 {
412   if (!oldpath || !newpath)
413     return -1;
414 
415   return _sos_syscall4(SOS_SYSCALL_ID_LINK,
416                        (unsigned int)oldpath,
417                        strlen(oldpath),
418                        (unsigned int)newpath,
419                        strlen(newpath));
420 }
421 
422 
423 int _sos_unlink(const char *pathname)
424 {
425   if (! pathname)
426     return -1;
427 
428   return _sos_syscall2(SOS_SYSCALL_ID_UNLINK,
429                        (unsigned int)pathname,
430                        strlen(pathname));
431 }
432 
433 
434 int _sos_rename (const char *oldpath, const char *newpath)
435 {
436   if (!oldpath || !newpath)
437     return -1;
438 
439   return _sos_syscall4(SOS_SYSCALL_ID_RENAME,
440                        (unsigned int)oldpath,
441                        strlen(oldpath),
442                        (unsigned int)newpath,
443                        strlen(newpath));
444 }
445 
446 
447 int _sos_symlink(const char *target, const char *path)
448 {
449   if (!path || !target)
450     return -1;
451 
452   return _sos_syscall4(SOS_SYSCALL_ID_SYMLINK,
453                        (unsigned int)path,
454                        strlen(path),
455                        (unsigned int)target,
456                        strlen(target));
457 }
458 
459 
460 int _sos_mknod(const char *pathname, mode_t mode,
461                int type,
462                unsigned int major, unsigned minor)
463 {
464   if (!pathname)
465     return -1;
466 
467   return _sos_syscall6(SOS_SYSCALL_ID_MKNOD,
468                        (unsigned int)pathname,
469                        strlen(pathname),
470                        type, mode, major, minor);
471 }
472 
473 
474 struct dirent; /* Forward declaration */
475 int _sos_readdir(int fd, struct dirent * dirent)
476 {
477   return _sos_syscall2(SOS_SYSCALL_ID_READDIR,
478                        fd,
479                        (unsigned int)dirent);
480 }
481 
482 
483 int _sos_mkdir(const char *pathname, mode_t mode)
484 {
485   if (!pathname)
486     return -1;
487 
488   return _sos_syscall3(SOS_SYSCALL_ID_MKDIR,
489                        (unsigned int)pathname,
490                        strlen(pathname),
491                        mode);
492 }
493 
494 
495 int _sos_rmdir(const char *pathname)
496 {
497   if (!pathname)
498     return -1;
499 
500   return _sos_syscall2(SOS_SYSCALL_ID_RMDIR,
501                        (unsigned int)pathname,
502                        strlen(pathname));
503 }
504 
505 
506 int _sos_chmod(const char *pathname, mode_t mode)
507 {
508   if (!pathname)
509     return -1;
510 
511   return _sos_syscall3(SOS_SYSCALL_ID_CHMOD,
512                        (unsigned int)pathname,
513                        strlen(pathname),
514                        mode);
515 }
516 
517 
518 int _sos_stat(const char *pathname, int nofollow, struct stat * st)
519 {
520   if (!pathname || !st)
521     return -1;
522 
523   return _sos_syscall4(SOS_SYSCALL_ID_STAT64,
524                        (unsigned int)pathname,
525                        strlen(pathname),
526                        nofollow,
527                        (unsigned int)st);
528 }
529 
530 
531 int _sos_chroot(const char *dirname)
532 {
533   if (!dirname)
534     return -1;
535 
536   return _sos_syscall2(SOS_SYSCALL_ID_CHROOT,
537                        (unsigned int)dirname,
538                        strlen(dirname));
539 }
540 
541 
542 int _sos_chdir(const char *dirname)
543 {
544   if (!dirname)
545     return -1;
546 
547   return _sos_syscall2(SOS_SYSCALL_ID_CHDIR,
548                        (unsigned int)dirname,
549                        strlen(dirname));
550 }
551 
552 
553 int _sos_fchdir(int fd)
554 {
555   return _sos_syscall1(SOS_SYSCALL_ID_FCHDIR,
556                        (unsigned int)fd);
557 }

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