source: trunk/libs/stdio.c @ 412

Last change on this file since 412 was 412, checked in by alain, 6 years ago

Introduce user libraries

File size: 7.3 KB
Line 
1/*
2 * stdio.c - User side system calls implementation.
3 *
4 * Author     Alain Greiner (2016,2017)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <shared_syscalls.h>
25#include <hal_user.h>
26#include <stdio.h>
27#include <stdlib.h>
28
29#define  reg_t     int
30
31///////////////////////
32void exit( int status )
33{
34    hal_user_syscall( SYS_EXIT,
35                      (reg_t)status, 0, 0, 0 );
36}
37
38// 11 ///////////////////////////
39int munmap( void         * addr,
40            unsigned int   size )
41{
42    return hal_user_syscall( SYS_MUNMAP,
43                             (reg_t)addr,
44                             (reg_t)size, 0, 0 );
45}
46
47// 12 //////////////////////////
48int open( const char * pathname,
49          int          flags,
50          int          mode )
51{
52    return hal_user_syscall( SYS_OPEN,
53                             (reg_t)pathname,
54                             (reg_t)flags,
55                             (reg_t)mode, 0 );
56}
57
58// 13 /////////////////////////
59void * mmap( void       * addr,
60             unsigned int length,
61             int          prot,
62             int          flags,
63             int          fd,
64             unsigned int offset )
65{
66    mmap_attr_t attr;
67    attr.addr   = addr;
68    attr.length = length;
69    attr.prot   = prot;
70    attr.flags  = flags;
71    attr.fdid   = fd;
72    attr.offset = offset;
73
74    if( hal_user_syscall( SYS_MMAP,
75                          (reg_t)&attr, 0, 0, 0 ) ) return NULL;
76    else                                            return attr.addr;
77}
78
79// 14 ////////////////////
80int read( int          fd,
81          void       * buf,
82          unsigned int count )
83{
84    return hal_user_syscall( SYS_READ,
85                             (reg_t)fd,
86                             (reg_t)buf,
87                             (reg_t)count, 0 );
88}
89
90// 15 /////////////////////
91int write( int          fd,
92           const void * buf,
93           unsigned int count )
94{
95    return hal_user_syscall( SYS_WRITE,
96                             (reg_t)fd,
97                             (reg_t)buf,
98                             (reg_t)count, 0 );
99}
100
101// 16 /////////////////////
102int lseek( int          fd,
103           unsigned int offset,
104           int          whence )
105{
106    return hal_user_syscall( SYS_LSEEK,
107                             (reg_t)fd,
108                             (reg_t)offset,
109                             (reg_t)whence, 0 );
110}
111
112// 17 /////////////
113int close( int fd )
114{
115    return hal_user_syscall( SYS_CLOSE,
116                             (reg_t)fd, 0, 0, 0 );
117}
118
119// 18 /////////////////////////////
120int unlink( const char * pathname )
121{
122    return hal_user_syscall( SYS_UNLINK,
123                             (reg_t)pathname, 0, 0, 0 );
124}
125
126// 19 ///////////////
127int pipe( int fd[2] )
128{
129    return -1;
130}
131
132// 20 ////////////////////////////
133int chdir( const char * pathname )
134{
135    return hal_user_syscall( SYS_CHDIR,
136                             (reg_t)pathname, 0, 0, 0 );
137}
138
139// 21 ///////////////////////////
140int mkdir( const char * pathname,
141           int          mode )
142{
143    return hal_user_syscall( SYS_MKDIR,
144                             (reg_t)pathname,
145                             (reg_t)mode, 0, 0 );
146}
147
148// 22 ////////////////////////////
149int mkfifo( const char * pathname,
150            int          mode )
151{
152    return hal_user_syscall( SYS_MKFIFO,
153                             (reg_t)pathname,
154                             (reg_t)mode, 0, 0 );
155}
156
157// 23 ////////////////////////////////
158DIR * opendir( const char * pathname )
159{
160    DIR   * dirp; 
161    int     error;
162    error = hal_user_syscall( SYS_OPENDIR,
163                              (reg_t)pathname,
164                              (reg_t)&dirp, 0, 0 );
165    if( error ) return NULL;
166    else        return dirp;
167}
168
169// 24 ///////////////////////////////
170struct dirent * readdir( DIR * dirp )
171{
172    struct dirent * dentp;
173    int             error;
174    error = hal_user_syscall( SYS_READDIR,
175                              (reg_t)dirp,
176                              (reg_t)&dentp, 0, 0 );
177    if( error ) return NULL;
178    else        return dentp;
179}
180
181// 25 ////////////////////
182int closedir( DIR * dirp )
183{
184    return hal_user_syscall( SYS_CLOSEDIR,
185                             (reg_t)dirp, 0, 0, 0 );
186}
187
188// 26 ///////////////////////
189int getcwd( char       * buf,
190            unsigned int bytes )
191{
192    return hal_user_syscall( SYS_GETCWD,
193                             (reg_t)buf,
194                             (reg_t)bytes, 0, 0 );
195}
196
197// 29 //////////////////////
198int rmdir( char * pathname )
199{
200    return hal_user_syscall( SYS_RMDIR,
201                             (reg_t)pathname, 0, 0, 0 );
202} 
203
204// 30 ///////////////////////////
205int utls( unsigned int operation,
206          unsigned int value )
207{
208    return hal_user_syscall( SYS_UTLS,
209                             (reg_t)operation,
210                             (reg_t)value, 0, 0 );
211}
212
213// 31 /////////////////////////
214int chmod( char     * pathname,
215           unsigned int   rights )
216{
217    return hal_user_syscall( SYS_CHMOD,
218                             (reg_t)pathname,
219                             (reg_t)rights, 0, 0 );
220}
221
222// 32 ///////////////////////////
223int signal( unsigned int   sigid,
224            void         * handler )
225{
226    return hal_user_syscall( SYS_SIGNAL,
227                             (reg_t)sigid,
228                             (reg_t)handler, 0, 0 );
229}
230
231// 33 /////////////////////////////////
232int gettimeofday( struct timeval  * tv,
233                  struct timezone * tz )
234{
235    return hal_user_syscall( SYS_SIGNAL,
236                             (reg_t)tv,
237                             (reg_t)tz, 0, 0 );
238}
239
240// 34 /////////////////////
241int kill( unsigned int pid,
242          unsigned int sig_id )
243{
244    return hal_user_syscall( SYS_KILL,
245                             (reg_t)pid,
246                             (reg_t)sig_id, 0, 0 );
247}
248
249// 35 //////
250int getpid()
251{
252    return hal_user_syscall( SYS_GETPID, 0, 0, 0, 0 );
253}
254
255// 36 ////
256int fork()
257{
258    return hal_user_syscall( SYS_FORK, 0, 0, 0, 0 );
259}
260
261// 37 /////////////////////
262int exec( char  * pathname,
263          char ** argv,
264          char ** envp )
265{
266    return hal_user_syscall( SYS_EXEC,
267                             (reg_t)pathname,
268                             (reg_t)argv,
269                             (reg_t)envp, 0 );
270}
271
272// 38 ///////////////////////////
273int stat( const char  * pathname,
274          struct stat * stat )
275{
276    return hal_user_syscall( SYS_EXEC,
277                             (reg_t)pathname,
278                             (reg_t)stat, 0, 0 );
279}
280
281// 39 ////////////////////////////
282int trace( unsigned int operation,
283           unsigned int pid, 
284           unsigned int trdid )
285{
286    return hal_user_syscall( SYS_TRACE,
287                             (reg_t)pid,
288                             (reg_t)trdid, 0, 0 );
289}
290
291
Note: See TracBrowser for help on using the repository browser.