source: soft/giet_vm/giet_libs/stdio.c @ 271

Last change on this file since 271 was 271, checked in by cfuguet, 10 years ago
  • Bugfix: The ISR_SWITCH index should be NB_PROCS_MAX + local_pid. This is because the first NB_PROCS_MAX indexes on the XICU in each cluster are used for the WAKEUP software interrupts.
  • Relocating the memcpy and memset functions into the giet_libs/stdlib.* files.
  • Modification of the sort application to used 8 threads instead of
    1. Modifying the mapping files to distribute the 8 threads on the available processors. (Ex. When using 4 processors, each one executes 2 threads)
  • Property svn:executable set to *
File size: 32.1 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////
2// File     : stdio.c         
3// Date     : 01/04/2010
4// Author   : alain greiner & Joel Porquet
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////
7// The stdio.c and stdio.h files are part of the GIET_VM nano-kernel.
8// This library contains all user-level functions that contain a system call
9// to access protected or shared ressources.
10///////////////////////////////////////////////////////////////////////////////////
11
12#include <stdarg.h>
13#include <stdio.h>
14#include <giet_config.h>
15
16////////////////////////////////////////////////////////////////////////////////////
17//////////////////////  MIPS32 related system calls  ///////////////////////////////
18////////////////////////////////////////////////////////////////////////////////////
19
20////////////////////////////////////////////////////////////////////////////////////
21// giet_procid()
22////////////////////////////////////////////////////////////////////////////////////
23// This function returns the processor identifier.
24////////////////////////////////////////////////////////////////////////////////////
25int giet_procid() 
26{
27    return sys_call( SYSCALL_PROCID,
28                     0, 0, 0, 0 );
29}
30////////////////////////////////////////////////////////////////////////////////////
31// giet_proctime()
32////////////////////////////////////////////////////////////////////////////////////
33// This function returns the local processor time (clock cycles since boot)
34////////////////////////////////////////////////////////////////////////////////////
35int giet_proctime() 
36{
37    return sys_call( SYSCALL_PROCTIME, 
38                     0, 0, 0, 0 );
39}
40
41
42////////////////////////////////////////////////////////////////////////////////////
43/////////////////////  TTY device related system calls /////////////////////////////
44////////////////////////////////////////////////////////////////////////////////////
45
46////////////////////////////////////////////////////////////////////////////////////
47// giet_tty_putc()
48////////////////////////////////////////////////////////////////////////////////////
49// This function displays a single ascii character on a terminal.
50// The terminal index must be defined in the task context in the boot phase.
51// It doesn't use the TTY_PUT_IRQ interrupt, and the associated kernel buffer.
52// - Returns 1 if the character has been written, 0 otherwise.
53////////////////////////////////////////////////////////////////////////////////////
54int giet_tty_putc(char byte) 
55{
56    return sys_call(SYSCALL_TTY_WRITE, 
57                    (unsigned int)(&byte),
58                    1, 
59                    0xFFFFFFFF, 
60                    0);
61}
62////////////////////////////////////////////////////////////////////////////////////
63// giet_tty_puts()
64////////////////////////////////////////////////////////////////////////////////////
65// This function displays a string on a terminal.
66// The terminal index must be defined in the task context in the boot phase.
67// The string must be terminated by a NUL character.
68// It doesn't use the TTY_PUT_IRQ interrupt, and the associated kernel buffer.
69// - Returns the number of written characters.
70////////////////////////////////////////////////////////////////////////////////////
71int giet_tty_puts(char * buf) 
72{
73    unsigned int length = 0;
74    while (buf[length] != 0) { length++; }
75    return sys_call(SYSCALL_TTY_WRITE, 
76                   (unsigned int)buf, 
77                   length, 
78                   0xFFFFFFFF, 
79                   0);
80}
81////////////////////////////////////////////////////////////////////////////////////
82// giet_tty_putw()
83////////////////////////////////////////////////////////////////////////////////////
84// This function displays the value of a 32-bit word with decimal characters.
85// The terminal index must be defined in the task context in the boot phase.
86// It doesn't use the TTY_PUT_IRQ interrupt, and the associated kernel buffer.
87// Returns the number of written characters (should be equal to ten).
88////////////////////////////////////////////////////////////////////////////////////
89int giet_tty_putw(unsigned int val) 
90{
91    char buf[10];
92    unsigned int i;
93    for (i = 0; i < 10; i++) 
94    {
95        buf[9 - i] = (val % 10) + 0x30;
96        val = val / 10;
97    }
98    return sys_call(SYSCALL_TTY_WRITE, 
99                   (unsigned int)buf,
100                   10,
101                   0xFFFFFFFF,
102                   0);
103}
104////////////////////////////////////////////////////////////////////////////////////
105// giet_tty_getc()
106////////////////////////////////////////////////////////////////////////////////////
107// This blocking function fetches a single ascii character from a terminal.
108// The terminal index must be defined in the task context in the boot phase.
109// It uses the IRQ_GET interrupt, and the associated kernel buffer.
110// - Returns 0 when completed.
111////////////////////////////////////////////////////////////////////////////////////
112int giet_tty_getc(char * byte) 
113{
114    unsigned int ret = 0;
115    while (ret == 0) 
116    {
117        ret = sys_call(SYSCALL_TTY_READ, 
118                      (unsigned int)byte,  // buffer address
119                      1,                   // number of characters
120                      0xFFFFFFFF,          // channel index from task context
121                      0);
122    }
123    return 0;
124}
125////////////////////////////////////////////////////////////////////////////////////
126// giet_tty_gets()
127////////////////////////////////////////////////////////////////////////////////////
128// This blocking function fetches a string from a terminal to a fixed length buffer.
129// The terminal index must be defined in the task context in the boot phase.
130// It uses the TTY_GET_IRQ interrupt, anf the associated kernel buffer.
131// - Returns 0 when completed.
132// - Up to (bufsize - 1) characters (including the non printable characters)
133//   will be copied into buffer, and the string is always completed by a NUL
134//   character.
135// - The <LF> character is interpreted, and the function close the string with a
136//   NUL character if <LF> is read.
137// - The <DEL> character is interpreted, and the corresponding character(s) are
138//   removed from the target buffer.
139////////////////////////////////////////////////////////////////////////////////////
140int giet_tty_gets( char*        buf, 
141                   unsigned int bufsize) 
142{
143    unsigned int ret;
144    unsigned char byte;
145    unsigned int index = 0;
146
147    while (index < (bufsize - 1)) 
148    {
149        do 
150        { 
151            ret = sys_call(SYSCALL_TTY_READ, 
152                           (unsigned int)(&byte),
153                           1,
154                           0xFFFFFFFF,
155                           0);
156        } 
157        while (ret != 1);
158
159        if (byte == 0x0A)  /* LF */
160        {
161            break; 
162        }
163        else if ((byte == 0x7F) && (index > 0))  /* DEL */
164        {
165            index--; 
166        }
167        else 
168        {
169            buf[index] = byte;
170            index++;
171        }
172    }
173    buf[index] = 0;
174    return 0;
175}
176////////////////////////////////////////////////////////////////////////////////////
177// giet_tty_getw()
178////////////////////////////////////////////////////////////////////////////////////
179// This blocking function fetches a string of decimal characters (most
180// significant digit first) to build a 32-bit unsigned integer.
181// The terminal index must be defined in the task context in the boot phase.
182// It uses the TTY_GET_IRQ interrupt, anf the associated kernel buffer.
183// - Returns necessarily 0 when completed.
184//
185// - The non-blocking system function _tty_read_irq is called several times,
186//   and the decimal characters are written in a 32 characters buffer until a
187//   <LF> character is read.
188// - The <DEL> character is interpreted, and previous characters can be
189//   cancelled. All others characters are ignored.
190// - When the <LF> character is received, the string is converted to an
191//   unsigned int value. If the number of decimal digit is too large for the 32
192//   bits range, the zero value is returned.
193////////////////////////////////////////////////////////////////////////////////////
194int giet_tty_getw(unsigned int * val) 
195{
196    unsigned char buf[32];
197    unsigned char byte;
198    unsigned int save = 0;
199    unsigned int dec = 0;
200    unsigned int done = 0;
201    unsigned int overflow = 0;
202    unsigned int max = 0;
203    unsigned int i;
204    unsigned int ret;
205
206    while (done == 0) 
207    {
208        do 
209        { 
210            ret = sys_call(SYSCALL_TTY_READ,
211                           (unsigned int)(&byte),
212                           1,
213                           0xFFFFFFFF,
214                           0); 
215        } 
216        while (ret != 1);
217
218        if ((byte > 0x2F) && (byte < 0x3A))  /* decimal character */
219        {
220            buf[max] = byte;
221            max++;
222            giet_tty_putc(byte);
223        }
224        else if ((byte == 0x0A))   /* LF */
225        {
226            done = 1;
227        }
228        else if (byte == 0x7F)   /* DEL */
229        {
230            if (max > 0) 
231            {
232                max--;      /* cancel the character */
233                giet_tty_putc(0x08);
234                giet_tty_putc(0x20);
235                giet_tty_putc(0x08);
236            }
237        }
238        if (max == 32)  /* decimal string overflow */
239        {
240            for (i = 0; i < max; i++) 
241            {
242                /* cancel the string */
243                giet_tty_putc(0x08);
244                giet_tty_putc(0x20);
245                giet_tty_putc(0x08);
246            }
247            giet_tty_putc(0x30);
248            *val = 0;      /* return 0 value */
249            return 0;
250        }
251    }
252
253    /* string conversion */
254    for (i = 0; i < max; i++) 
255    {
256        dec = dec * 10 + (buf[i] - 0x30);
257        if (dec < save)  overflow = 1; 
258        save = dec;
259    }
260
261    /* check overflow */
262    if (overflow == 0) 
263    {
264        *val = dec; /* return decimal value */
265    }
266    else 
267    {
268        for (i = 0; i < max; i++) 
269        {
270            /* cancel the string */
271            giet_tty_putc(0x08);
272            giet_tty_putc(0x20);
273            giet_tty_putc(0x08);
274        }
275        giet_tty_putc(0x30);
276        *val = 0;         /* return 0 value */
277    }
278    return 0;
279}
280////////////////////////////////////////////////////////////////////////////////////
281// giet_tty_printf()
282////////////////////////////////////////////////////////////////////////////////////
283// This function is a simplified version of the mutek_printf() function.
284// The terminal index must be defined in the calling task context.
285// It doesn't use the IRQ_PUT interrupt, and the associated kernel buffer.
286// Only a limited number of formats are supported:
287//   - %d : signed decimal
288//   - %u : unsigned decimal
289//   - %x : hexadecimal
290//   - %c : char
291//   - %s : string
292// - Returns 0 if success, > 0 if error.
293////////////////////////////////////////////////////////////////////////////////////
294int giet_tty_printf(char * format, ...) 
295{
296    va_list ap;
297    va_start(ap, format);
298    unsigned int ret;
299
300    if (GIET_MONO_TTY)
301    {
302        ret = sys_call(SYSCALL_TTY_LOCK, 0, 0, 0, 0); // Get TTY lock
303    }
304
305printf_text:
306
307    while (*format) 
308    {
309        unsigned int i;
310        for (i = 0 ; format[i] && (format[i] != '%') ; i++);
311        if (i) 
312        {
313            ret = sys_call(SYSCALL_TTY_WRITE, 
314                           (unsigned int)format,
315                           i, 
316                           0xFFFFFFFF,
317                           0);
318
319            if (ret != i) goto return_error;
320
321            format += i;
322        }
323        if (*format == '%') 
324        {
325            format++;
326            goto printf_arguments;
327        }
328    }
329
330    if (GIET_MONO_TTY)
331    {
332        ret = sys_call(SYSCALL_TTY_LOCK, 1, 0, 0, 0); // Release TTY lock
333    }
334
335    va_end(ap);
336    return 0;
337
338printf_arguments:
339
340    {
341        int val = va_arg(ap, long);
342        char buf[20];
343        char * pbuf;
344        unsigned int len = 0;
345        static const char HexaTab[] = "0123456789ABCDEF";
346        unsigned int i;
347
348        switch (*format++) {
349            case ('c'):             /* char conversion */
350                len = 1;
351                buf[0] = val;
352                pbuf = buf;
353                break;
354            case ('d'):             /* decimal signed integer */
355                if (val < 0) 
356                {
357                    val = -val;
358                    ret = sys_call(SYSCALL_TTY_WRITE, 
359                                   (unsigned int)"-",
360                                   1,
361                                   0xFFFFFFFF,
362                                   0);
363                    if (ret != 1) goto return_error;
364                }
365            case ('u'):             /* decimal unsigned integer */
366                for(i = 0; i < 10; i++) 
367                {
368                    buf[9 - i] = HexaTab[val % 10];
369                    if (!(val /= 10)) break;
370                }
371                len =  i + 1;
372                pbuf = &buf[9 - i];
373                break;
374            case ('x'):             /* hexadecimal integer */
375                ret = sys_call(SYSCALL_TTY_WRITE,
376                               (unsigned int)"0x",
377                               2,
378                               0xFFFFFFFF,
379                               0);
380                if (ret != 2) goto return_error;       /* return error */
381                for(i = 0; i < 8; i++) 
382                {
383                    buf[7 - i] = HexaTab[val % 16U];
384                    if (!(val /= 16U))  break;
385                }
386                len =  i + 1;
387                pbuf = &buf[7 - i];
388                break;
389            case ('s'):             /* string */
390                {
391                    char * str = (char *) val;
392                    while (str[len]) 
393                    {
394                        len++;
395                    }
396                    pbuf = (char *) val;
397                }
398                break;
399            default:
400                goto printf_text;
401        }
402
403        ret = sys_call(SYSCALL_TTY_WRITE, 
404                       (unsigned int)pbuf,
405                       len,
406                       0xFFFFFFFF,
407                       0);
408        if (ret != len)  goto return_error;
409       
410        goto printf_text;
411    }
412
413return_error:
414    if (GIET_MONO_TTY)
415    {
416        ret = sys_call(SYSCALL_TTY_LOCK, 1, 0, 0, 0); // Release TTY lock
417    }
418
419    return 1;
420}
421
422
423
424//////////////////////////////////////////////////////////////////////////////////
425/////////////////////  TIMER related system calls ////////////////////////////////
426//////////////////////////////////////////////////////////////////////////////////
427
428//////////////////////////////////////////////////////////////////////////////////
429// giet_timer_start()
430//////////////////////////////////////////////////////////////////////////////////
431// This function activates the private user timer allocated to the calling task
432// in the boot phase.
433// - Returns 0 if success, > 0 if error.
434//////////////////////////////////////////////////////////////////////////////////
435int giet_timer_start() 
436{
437    return sys_call( SYSCALL_TIMER_START, 
438                     0, 0, 0, 0 );
439}
440//////////////////////////////////////////////////////////////////////////////////
441// giet_timer_stop()
442//////////////////////////////////////////////////////////////////////////////////
443// This function activates the user timer allocated to the calling task.
444// - Returns 0 if success, > 0 if error.
445//////////////////////////////////////////////////////////////////////////////////
446int giet_timer_stop() 
447{
448    return sys_call( SYSCALL_TIMER_STOP, 
449                     0, 0, 0, 0 );
450}
451
452
453//////////////////////////////////////////////////////////////////////////////////
454///////////////  Frame buffer device related system calls  ///////////////////////
455//////////////////////////////////////////////////////////////////////////////////
456
457//////////////////////////////////////////////////////////////////////////////////
458// giet_fb_sync_write()
459//////////////////////////////////////////////////////////////////////////////////
460// This blocking function use a memory copy strategy to transfer data from a
461// user buffer to the frame buffer device in kernel space.
462//     offset : offset (in bytes) in the frame buffer
463//     buffer : base address of the memory buffer
464//     length : number of bytes to be transfered
465// - Returns 0 if success, > 0 if error (e.g. memory buffer not in user space).
466//////////////////////////////////////////////////////////////////////////////////
467int giet_fb_sync_write( unsigned int offset, 
468                        void *       buffer, 
469                        unsigned int length ) 
470{
471    return sys_call( SYSCALL_FB_SYNC_WRITE, 
472                     offset, 
473                     (unsigned int)buffer, 
474                     length, 
475                     0 );
476}
477//////////////////////////////////////////////////////////////////////////////////
478// giet_fb_sync_read()
479//////////////////////////////////////////////////////////////////////////////////
480// This blocking function use a memory copy strategy to transfer data from the
481// frame buffer device in kernel space to an user buffer.
482//     offset : offset (in bytes) in the frame buffer
483//     buffer : base address of the user buffer
484//     length : number of bytes to be transfered
485// - Returns 0 if success, > 0 if error (e.g. memory buffer not in user space).
486//////////////////////////////////////////////////////////////////////////////////
487int giet_fb_sync_read( unsigned int offset, 
488                       void *       buffer, 
489                       unsigned int length ) 
490{
491    return sys_call( SYSCALL_FB_SYNC_READ, 
492                     offset, 
493                     (unsigned int)buffer, 
494                     length, 
495                     0 );
496}
497//////////////////////////////////////////////////////////////////////////////////
498// giet_fb_cma_init()
499//////////////////////////////////////////////////////////////////////////////////
500// This function initializes the two chbuf SRC an DST used by the CMA controller
501// and activates the CMA channel allocated to the calling task.
502// - buf0   : first user buffer virtual address
503// - buf0   : second user buffer virtual address
504// - length : buffer size (bytes)
505// - Returns 0 if success, > 0 if error.
506//////////////////////////////////////////////////////////////////////////////////
507int giet_fb_cma_init( void *       buf0,
508                      void *       buf1,
509                      unsigned int length )
510{
511    return sys_call( SYSCALL_FB_CMA_INIT, 
512                     (unsigned int)buf0, 
513                     (unsigned int)buf1, 
514                     length, 
515                     0 );
516}
517//////////////////////////////////////////////////////////////////////////////////
518// giet_fb_cma_write()
519//////////////////////////////////////////////////////////////////////////////////
520// This function set the valid status for one of the SRC user buffer.
521// and reset the valid status for the DST frame buffer.
522// - bufffer_id : 0 => buf0 valid is set / not 0  => buf1 valid is set
523// - Returns 0 if success, > 0 if error.
524//////////////////////////////////////////////////////////////////////////////////
525int giet_fb_cma_write( unsigned int buffer_id )
526{
527    return sys_call( SYSCALL_FB_CMA_WRITE, 
528                     buffer_id, 
529                     0, 0, 0 );
530}
531//////////////////////////////////////////////////////////////////////////////////
532// giet_fb_cma_stop()
533//////////////////////////////////////////////////////////////////////////////////
534// This function desactivates the CMA channel allocated to the calling task.
535// - Returns 0 if success, > 0 if error.
536//////////////////////////////////////////////////////////////////////////////////
537int giet_fb_cma_stop( )
538{
539    return sys_call( SYSCALL_FB_CMA_STOP, 
540                     0, 0, 0, 0 );
541}
542
543
544//////////////////////////////////////////////////////////////////////////////////
545/////////////////////// NIC related system calls /////////////////////////////////
546//////////////////////////////////////////////////////////////////////////////////
547
548//////////////////////////////////////////////////////////////////////////////////
549// giet_nic_cma_init()
550//////////////////////////////////////////////////////////////////////////////////
551// This function initializes the memory chbuf used by the CMA controller,
552// activates the NIC channel allocated to the calling task, and the CMA channel.
553// - tx     : RX channel if 0 / TX channel if non 0
554// - buf0   : first user buffer virtual address
555// - buf1   : second user buffer virtual address
556// - length : buffer size (bytes)
557// - Returns 0 if success, > 0 if error
558//////////////////////////////////////////////////////////////////////////////////
559int giet_nic_cma_start()
560{
561    return sys_call( SYSCALL_NIC_CMA_START,
562                     0, 0, 0, 0 );
563}
564//////////////////////////////////////////////////////////////////////////////////
565// giet_nic_cma_stop()
566//////////////////////////////////////////////////////////////////////////////////
567// This function desactivates the NIC channel and the two CMA channels
568// allocated to the calling task.
569// - Returns 0 if success, > 0 if error.
570//////////////////////////////////////////////////////////////////////////////////
571int giet_nic_cma_stop( )
572{
573    return sys_call( SYSCALL_NIC_CMA_STOP,
574                     0, 0, 0, 0 );
575}
576
577
578//////////////////////////////////////////////////////////////////////////////////
579///////////////////// Miscellaneous system calls /////////////////////////////////
580//////////////////////////////////////////////////////////////////////////////////
581
582///////////////////////////////////////////////////////////////////////////////////
583// giet_assert()
584///////////////////////////////////////////////////////////////////////////////////
585// This function uses the giet_tty_puts() and giet_exit() system calls.
586///////////////////////////////////////////////////////////////////////////////////
587void giet_assert( unsigned int condition,
588                  char*        string )
589{
590    if ( condition == 0 )
591    {
592        giet_tty_puts( string );
593        giet_exit();
594    }
595}
596//////////////////////////////////////////////////////////////////////////////////
597// giet_vobj_get_vbase()
598//////////////////////////////////////////////////////////////////////////////////
599// This function writes in argument (vobj_vaddr) the virtual base address
600// of a vobj (defined in the mapping_info data structure), identified by
601// the two arguments (vspace_name and vobj_name).
602// The (vobj_type) argument is redundant, and used for coherence checking.
603// - Returns the address if success,  0 if error ( not defined or wrong type )
604//////////////////////////////////////////////////////////////////////////////////
605int giet_vobj_get_vbase( char*         vspace_name, 
606                         char*         vobj_name, 
607                         unsigned int  vobj_type, 
608                         unsigned int* vobj_vaddr ) 
609{
610    return sys_call( SYSCALL_VOBJ_GET_VBASE, 
611                     (unsigned int) vspace_name,
612                     (unsigned int) vobj_name,
613                     (unsigned int) vobj_type,
614                     (unsigned int) vobj_vaddr );
615}
616////////////////////////////////////////////////////////////////////////////////////
617// giet_proc_number()
618////////////////////////////////////////////////////////////////////////////////////
619// This function returns in the buffer argument the number of processors
620// in the cluster specified by the cluster_id argument.
621// - Returns 0 if success, > 0 if error ( cluster index too large )
622////////////////////////////////////////////////////////////////////////////////////
623int giet_proc_number( unsigned int  cluster_id, 
624                      unsigned int* buffer ) 
625{
626    return sys_call(SYSCALL_PROC_NUMBER, cluster_id, (unsigned int) buffer, 0, 0);
627}
628//////////////////////////////////////////////////////////////////////////////////
629// giet_exit()
630//////////////////////////////////////////////////////////////////////////////////
631// This function stops execution of the calling task with a TTY message,
632// the user task is descheduled and becomes not runable.
633// It does not consume processor cycles anymore.
634//////////////////////////////////////////////////////////////////////////////////
635void giet_exit() 
636{
637    sys_call( SYSCALL_EXIT,
638              0, 0, 0, 0 );
639}
640//////////////////////////////////////////////////////////////////////////////////
641// giet_context_switch()
642//////////////////////////////////////////////////////////////////////////////////
643// The user task calling this function is descheduled and
644// the processor is allocated to another task.
645//////////////////////////////////////////////////////////////////////////////////
646int giet_context_switch() 
647{
648    return sys_call( SYSCALL_CTX_SWITCH,
649                     0, 0, 0, 0 );
650}
651//////////////////////////////////////////////////////////////////////////////////
652// giet_proc_task_id()
653//////////////////////////////////////////////////////////////////////////////////
654// This functions returns the local task id.
655// If processor has n tasks the local task index is ranging from 0 to n-1
656//////////////////////////////////////////////////////////////////////////////////
657int giet_proc_task_id() 
658{
659    return sys_call( SYSCALL_LOCAL_TASK_ID, 
660                     0, 0, 0, 0 );
661}
662//////////////////////////////////////////////////////////////////////////////////
663// giet_heap_info()
664//////////////////////////////////////////////////////////////////////////////////
665// This function returns the base address and size of the current task's heap
666//////////////////////////////////////////////////////////////////////////////////
667int giet_heap_info( unsigned int* vaddr, 
668                             unsigned int* length ) 
669{
670    return sys_call( SYSCALL_HEAP_INFO, 
671                     (unsigned int)vaddr, 
672                     (unsigned int)length, 
673                     0, 0 );
674}
675//////////////////////////////////////////////////////////////////////////////////
676// giet_global_task_id()
677//////////////////////////////////////////////////////////////////////////////////
678// This functions returns the global task id, which is unique in all the giet.
679//////////////////////////////////////////////////////////////////////////////////
680int giet_global_task_id() 
681{
682    return sys_call( SYSCALL_GLOBAL_TASK_ID, 
683                     0, 0, 0, 0 );
684}
685
686//////////////////////////////////////////////////////////////////////////////////
687// giet_thread_id()
688//////////////////////////////////////////////////////////////////////////////////
689// This functions returns the thread index of the current task.
690//////////////////////////////////////////////////////////////////////////////////
691int giet_thread_id() 
692{
693    return sys_call( SYSCALL_THREAD_ID, 
694                     0, 0, 0, 0 );
695}
696
697///////////////////////////////////////////////////////////////////////////////////
698///////////////////// FAT related system calls ////////////////////////////////////
699///////////////////////////////////////////////////////////////////////////////////
700
701///////////////////////////////////////////////////////////////////////////////////
702// giet_fat_open()
703///////////////////////////////////////////////////////////////////////////////////
704// Open a file identified by a pathname, and contained in the system FAT.
705// The read/write flags are not supported yet: no effect.
706///////////////////////////////////////////////////////////////////////////////////
707int giet_fat_open( const char*   pathname, 
708                   unsigned int  flags )
709{
710    return sys_call( SYSCALL_FAT_OPEN, 
711                     (unsigned int)pathname, 
712                     flags,
713                     0, 0 );
714}
715///////////////////////////////////////////////////////////////////////////////////
716// giet_fat_read()
717///////////////////////////////////////////////////////////////////////////////////
718// Read "count" sectors from a file identified by "fd", skipping "offset"
719// sectors in file, and writing into the user "buffer".
720// The user buffer base address shoulb be 64 bytes aligned.
721///////////////////////////////////////////////////////////////////////////////////
722// This system call specification should evolve to the UNIX specification:
723// - count must be a number of bytes, with no alignment constraint on user buffer.
724// - offset argument should be removed and replaced by an implicit "lseek" pointer
725//   stored in the file descriptor.
726// This suppose to implement a sectors cache
727///////////////////////////////////////////////////////////////////////////////////
728int giet_fat_read( unsigned int fd, 
729                   void*        buffer, 
730                   unsigned int count,
731                   unsigned int offset )
732{
733    return sys_call( SYSCALL_FAT_READ,
734                     fd, 
735                     (unsigned int)buffer,
736                     count, 
737                     offset );
738}
739///////////////////////////////////////////////////////////////////////////////////
740// giet_fat_write()
741///////////////////////////////////////////////////////////////////////////////////
742// Write "count" sectors from a file identified by "fd", skipping "offset"
743// sectors in file, and reading from the user "buffer".
744// The user buffer base address shoulb be 64 bytes aligned.
745///////////////////////////////////////////////////////////////////////////////////
746// This system call specification should evolve to the UNIX specification:
747// - count must be a number of bytes, with no alignment constraint on buffer
748// - offset argument should be removed and replaced by an implicit "lseek" pointer
749//   stored in the file descriptor.
750// This suppose to implement a sectors cache
751///////////////////////////////////////////////////////////////////////////////////
752int giet_fat_write( unsigned int fd,
753                    void*        buffer, 
754                    unsigned int count,
755                    unsigned int offset )
756{
757    return sys_call( SYSCALL_FAT_WRITE, 
758                     fd, 
759                     (unsigned int)buffer,
760                     count, 
761                     offset );
762}
763///////////////////////////////////////////////////////////////////////////////////
764// giet_fat_lseek()
765///////////////////////////////////////////////////////////////////////////////////
766// Change the lseek file pointer value for a file identified by "fd".
767///////////////////////////////////////////////////////////////////////////////////
768int giet_fat_lseek( unsigned int fd, 
769                    unsigned int offset, 
770                    unsigned int whence)
771{
772    return sys_call( SYSCALL_FAT_LSEEK, 
773                     fd, 
774                     offset, 
775                     whence, 
776                     0 );
777}
778
779///////////////////////////////////////////////////////////////////////////////////
780// giet_fat_fstat()
781///////////////////////////////////////////////////////////////////////////////////
782// Return stats of a file identified by "fd".
783// (Only the file_size in sectors for this moment)
784///////////////////////////////////////////////////////////////////////////////////
785int giet_fat_fstat( unsigned int fd )
786{
787    return sys_call( SYSCALL_FAT_FSTAT,
788                     fd,
789                     0, 0, 0 );
790}
791
792///////////////////////////////////////////////////////////////////////////////////
793// giet_fat_close()
794///////////////////////////////////////////////////////////////////////////////////
795// Close a file identified by "fd".
796///////////////////////////////////////////////////////////////////////////////////
797int giet_fat_close( unsigned int fd )
798{
799    return sys_call( SYSCALL_FAT_CLOSE,
800                     fd,
801                     0, 0, 0 );
802}
803
804
805///////////////////////////////////////////////////////////////////////////////////
806////////////////// Pseudo system calls (no syscall instruction) ///////////////////
807///////////////////////////////////////////////////////////////////////////////////
808
809///////////////////////////////////////////////////////////////////////////////////
810// giet_rand()
811// This function returns a pseudo-random value derived from the processor cycle
812// count. This value is comprised between 0 & 65535.
813///////////////////////////////////////////////////////////////////////////////////
814int giet_rand() 
815{
816    unsigned int x = sys_call(SYSCALL_PROCTIME, 0, 0, 0, 0);
817    if ((x & 0xF) > 7) {
818        return (x*x & 0xFFFF);
819    }
820    else {
821        return (x*x*x & 0xFFFF);
822    }
823}
824
825// Local Variables:
826// tab-width: 4
827// c-basic-offset: 4
828// c-file-offsets:((innamespace . 0)(inline-open . 0))
829// indent-tabs-mode: nil
830// End:
831// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
832
Note: See TracBrowser for help on using the repository browser.