OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libc/] [sys.tex] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 lampret
@c                                           -*- Texinfo -*-
2
@node Syscalls
3
@chapter System Calls
4
 
5
@cindex linking the C library
6
The C subroutine library depends on a handful of subroutine calls for
7
operating system services.  If you use the C library on a system that
8
complies with the POSIX.1 standard (also known as IEEE 1003.1), most of
9
these subroutines are supplied with your operating system.
10
 
11
If some of these subroutines are not provided with your system---in
12
the extreme case, if you are developing software for a ``bare board''
13
system, without an OS---you will at least need to provide do-nothing
14
stubs (or subroutines with minimal functionality) to allow your
15
programs to link with the subroutines in @code{libc.a}.
16
 
17
@menu
18
* Stubs::               Definitions for OS interface
19
* Reentrant Syscalls::  Reentrant covers for OS subroutines
20
@end menu
21
 
22
@node Stubs
23
@section Definitions for OS interface
24
@cindex stubs
25
 
26
@cindex subroutines for OS interface
27
@cindex OS interface subroutines
28
This is the complete set of system definitions (primarily subroutines)
29
required; the examples shown implement the minimal functionality
30
required to allow @code{libc} to link, and fail gracefully where OS
31
services are not available.
32
 
33
Graceful failure is permitted by returning an error code.  A minor
34
complication arises here: the C library must be compatible with
35
development environments that supply fully functional versions of these
36
subroutines.  Such environments usually return error codes in a global
37
@code{errno}.  However, the Cygnus C library provides a @emph{macro}
38
definition for @code{errno} in the header file @file{errno.h}, as part
39
of its support for reentrant routines (@pxref{Reentrancy,,Reentrancy}).
40
 
41
@cindex @code{errno} global vs macro
42
The bridge between these two interpretations of @code{errno} is
43
straightforward: the C library routines with OS interface calls
44
capture the @code{errno} values returned globally, and record them in
45
the appropriate field of the reentrancy structure (so that you can query
46
them using the @code{errno} macro from @file{errno.h}).
47
 
48
This mechanism becomes visible when you write stub routines for OS
49
interfaces.   You must include @file{errno.h}, then disable the macro,
50
like this:
51
 
52
@example
53
#include <errno.h>
54
#undef errno
55
extern int errno;
56
@end example
57
 
58
@noindent
59
The examples in this chapter include this treatment of @code{errno}.
60
 
61
@ftable @code
62
@item _exit
63
Exit a program without cleaning up files.  If your system doesn't
64
provide this, it is best to avoid linking with subroutines that require
65
it (@code{exit}, @code{system}).
66
 
67
@item close
68
Close a file.  Minimal implementation:
69
 
70
@example
71
int close(int file)@{
72
    return -1;
73
@}
74
@end example
75
 
76
@item environ
77
A pointer to a list of environment variables and their values.  For a
78
minimal environment, this empty list is adequate:
79
 
80
@example
81
char *__env[1] = @{ 0 @};
82
char **environ = __env;
83
@end example
84
 
85
@item execve
86
Transfer control to a new process.  Minimal implementation (for a system
87
without processes):
88
 
89
@example
90
#include <errno.h>
91
#undef errno
92
extern int errno;
93
int execve(char *name, char **argv, char **env)@{
94
  errno=ENOMEM;
95
  return -1;
96
@}
97
@end example
98
 
99
@item fork
100
Create a new process.  Minimal implementation (for a system without processes):
101
 
102
@example
103
#include <errno.h>
104
#undef errno
105
extern int errno;
106
int fork() @{
107
  errno=EAGAIN;
108
  return -1;
109
@}
110
@end example
111
 
112
@item fstat
113
Status of an open file.  For consistency with other minimal
114
implementations in these examples, all files are regarded as character
115
special devices.  The @file{sys/stat.h} header file required is
116
distributed in the @file{include} subdirectory for this C library.
117
 
118
@example
119
#include <sys/stat.h>
120
int fstat(int file, struct stat *st) @{
121
  st->st_mode = S_IFCHR;
122
  return 0;
123
@}
124
@end example
125
 
126
@item getpid
127
Process-ID; this is sometimes used to generate strings unlikely to
128
conflict with other processes.  Minimal implementation, for a system
129
without processes:
130
 
131
@example
132
int getpid() @{
133
  return 1;
134
@}
135
@end example
136
 
137
@item isatty
138
Query whether output stream is a terminal.   For consistency with the
139
other minimal implementations, which only support output to
140
@code{stdout}, this minimal implementation is suggested:
141
 
142
@example
143
int isatty(int file)@{
144
   return 1;
145
@}
146
@end example
147
 
148
@item kill
149
Send a signal.  Minimal implementation:
150
 
151
@example
152
#include <errno.h>
153
#undef errno
154
extern int errno;
155
int kill(int pid, int sig)@{
156
  errno=EINVAL;
157
  return(-1);
158
@}
159
@end example
160
 
161
@item link
162
Establish a new name for an existing file.  Minimal implementation:
163
 
164
@example
165
#include <errno.h>
166
#undef errno
167
extern int errno;
168
int link(char *old, char *new)@{
169
  errno=EMLINK;
170
  return -1;
171
@}
172
@end example
173
 
174
@item lseek
175
Set position in a file.  Minimal implementation:
176
 
177
@example
178
int lseek(int file, int ptr, int dir)@{
179
    return 0;
180
@}
181
@end example
182
 
183
@c FIXME! Why no stub for open?
184
 
185
@item read
186
Read from a file.  Minimal implementation:
187
 
188
@example
189
int read(int file, char *ptr, int len)@{
190
    return 0;
191
@}
192
@end example
193
 
194
@item sbrk
195
Increase program data space.  As @code{malloc} and related functions
196
depend on this, it is useful to have a working implementation.  The
197
following suffices for a standalone system; it exploits the symbol
198
@code{end} automatically defined by the GNU linker.
199
 
200
@example
201
@group
202
caddr_t sbrk(int incr)@{
203
  extern char end;              /* @r{Defined by the linker} */
204
  static char *heap_end;
205
  char *prev_heap_end;
206
 
207
  if (heap_end == 0) @{
208
    heap_end = &end;
209
  @}
210
  prev_heap_end = heap_end;
211
  if (heap_end + incr > stack_ptr)
212
    @{
213
      _write (1, "Heap and stack collision\n", 25);
214
      abort ();
215
    @}
216
 
217
  heap_end += incr;
218
  return (caddr_t) prev_heap_end;
219
@}
220
@end group
221
@end example
222
 
223
@item stat
224
Status of a file (by name).  Minimal implementation:
225
 
226
@example
227
int stat(char *file, struct stat *st) @{
228
  st->st_mode = S_IFCHR;
229
  return 0;
230
@}
231
@end example
232
 
233
@item times
234
Timing information for current process.  Minimal implementation:
235
 
236
@example
237
int times(struct tms *buf)@{
238
  return -1;
239
@}
240
@end example
241
 
242
@item unlink
243
Remove a file's directory entry.  Minimal implementation:
244
 
245
@example
246
#include <errno.h>
247
#undef errno
248
extern int errno;
249
int unlink(char *name)@{
250
  errno=ENOENT;
251
  return -1;
252
@}
253
@end example
254
 
255
@item wait
256
Wait for a child process.  Minimal implementation:
257
@example
258
#include <errno.h>
259
#undef errno
260
extern int errno;
261
int wait(int *status) @{
262
  errno=ECHILD;
263
  return -1;
264
@}
265
@end example
266
 
267
@item write
268
Write a character to a file.  @file{libc} subroutines will use this
269
system routine for output to all files, @emph{including}
270
@code{stdout}---so if you need to generate any output, for example to a
271
serial port for debugging, you should make your minimal @code{write}
272
capable of doing this.  The following minimal implementation is an
273
incomplete example; it relies on a @code{writechar} subroutine (not
274
shown; typically, you must write this in assembler from examples
275
provided by your hardware manufacturer) to actually perform the output.
276
 
277
@example
278
@group
279
int write(int file, char *ptr, int len)@{
280
    int todo;
281
 
282
    for (todo = 0; todo < len; todo++) @{
283
        writechar(*ptr++);
284
    @}
285
    return len;
286
@}
287
@end group
288
@end example
289
 
290
@end ftable
291
 
292
@page
293
@node Reentrant Syscalls
294
@section Reentrant covers for OS subroutines
295
 
296
Since the system subroutines are used by other library routines that
297
require reentrancy, @file{libc.a} provides cover routines (for example,
298
the reentrant version of @code{fork} is @code{_fork_r}).  These cover
299
routines are consistent with the other reentrant subroutines in this
300
library, and achieve reentrancy by using a reserved global data block
301
(@pxref{Reentrancy,,Reentrancy}).
302
 
303
@c FIXME!!! The following ignored text specifies how this section ought
304
@c to work;  however, both standalone info and Emacs info mode fail when
305
@c confronted with nodes beginning `_' as of 24may93.  Restore when Info
306
@c readers fixed!
307
@ignore
308
@menu
309
* _open_r::     Reentrant version of open
310
* _close_r::    Reentrant version of close
311
* _lseek_r::    Reentrant version of lseek
312
* _read_r::     Reentrant version of read
313
* _write_r::    Reentrant version of write
314
* _link_r::     Reentrant version of link
315
* _unlink_r::   Reentrant version of unlink
316
* _stat_r::     Reentrant version of stat
317
* _fstat_r::    Reentrant version of fstat
318
* _sbrk_r::     Reentrant version of sbrk
319
* _fork_r::     Reentrant version of fork
320
* _wait_r::     Reentrant version of wait
321
@end menu
322
 
323
@down
324
@include reent/filer.def
325
@include reent/execr.def
326
@include reent/statr.def
327
@include reent/fstatr.def
328
@include reent/linkr.def
329
@include reent/sbrkr.def
330
@up
331
@end ignore
332
 
333
@ftable @code
334
@item _open_r
335
A reentrant version of @code{open}.  It takes a pointer
336
to the global data block, which holds @code{errno}.
337
 
338
@example
339
int _open_r(void *@var{reent},
340
    const char *@var{file}, int @var{flags}, int @var{mode});
341
@end example
342
 
343
@item _close_r
344
A reentrant version of @code{close}.  It takes a pointer to the global
345
data block, which holds @code{errno}.
346
 
347
@example
348
int _close_r(void *@var{reent}, int @var{fd});
349
@end example
350
 
351
@item _lseek_r
352
A reentrant version of @code{lseek}.  It takes a pointer to the global
353
data block, which holds @code{errno}.
354
 
355
@example
356
off_t _lseek_r(void *@var{reent},
357
    int @var{fd}, off_t @var{pos}, int @var{whence});
358
@end example
359
 
360
@item _read_r
361
A reentrant version of @code{read}.  It takes a pointer to the global
362
data block, which holds @code{errno}.
363
 
364
@example
365
long _read_r(void *@var{reent},
366
    int @var{fd}, void *@var{buf}, size_t @var{cnt});
367
@end example
368
 
369
@item _write_r
370
A reentrant version of @code{write}.  It takes a pointer to the global
371
data block, which holds @code{errno}.
372
 
373
@example
374
long _write_r(void *@var{reent},
375
    int @var{fd}, const void *@var{buf}, size_t @var{cnt});
376
@end example
377
 
378
@item _fork_r
379
A reentrant version of @code{fork}.  It takes a pointer to the global
380
data block, which holds @code{errno}.
381
 
382
@example
383
int _fork_r(void *@var{reent});
384
@end example
385
 
386
@item _wait_r
387
A reentrant version of @code{wait}.  It takes a pointer to the global
388
data block, which holds @code{errno}.
389
 
390
@example
391
int _wait_r(void *@var{reent}, int *@var{status});
392
@end example
393
 
394
@item _stat_r
395
A reentrant version of @code{stat}.  It takes a pointer to the global
396
data block, which holds @code{errno}.
397
 
398
@example
399
int _stat_r(void *@var{reent},
400
    const char *@var{file}, struct stat *@var{pstat});
401
@end example
402
 
403
@item _fstat_r
404
A reentrant version of @code{fstat}.  It takes a pointer to the global
405
data block, which holds @code{errno}.
406
 
407
@example
408
int _fstat_r(void *@var{reent},
409
    int @var{fd}, struct stat *@var{pstat});
410
@end example
411
 
412
@item _link_r
413
A reentrant version of @code{link}.  It takes a pointer to the global
414
data block, which holds @code{errno}.
415
 
416
@example
417
int _link_r(void *@var{reent},
418
    const char *@var{old}, const char *@var{new});
419
@end example
420
 
421
@item _unlink_r
422
A reentrant version of @code{unlink}.  It takes a pointer to the global
423
data block, which holds @code{errno}.
424
 
425
@example
426
int _unlink_r(void *@var{reent}, const char *@var{file});
427
@end example
428
 
429
@item _sbrk_r
430
A reentrant version of @code{sbrk}.  It takes a pointer to the global
431
data block, which holds @code{errno}.
432
 
433
@example
434
char *_sbrk_r(void *@var{reent}, size_t @var{incr});
435
@end example
436
@end ftable

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.