Skip to content

Commit d88d410

Browse files
committed
Template for "Ring 3 and Syscalls"
1 parent a87b462 commit d88d410

12 files changed

Lines changed: 175 additions & 53 deletions

File tree

include/prototypes.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#include "syscalls.h"
2+
3+
extern uint32 do_syscall(uint32 id, uint32 args_count, ...);
4+
15
/* in file addargs.c */
26
extern status addargs(pid32, int32, int32[], int32,char *, void *);
37

@@ -28,9 +32,13 @@ extern syscall close(did32);
2832

2933
/* in file control.c */
3034
extern syscall control(did32, int32, int32, int32);
35+
#define syscall_control(...) \
36+
do_generic_syscall(syscall, SYSCALL_CONTROL, __VA_ARGS__)
3137

3238
/* in file create.c */
3339
extern pid32 create(void *, uint32, pri16, char *, uint32, ...);
40+
#define syscall_create(...) \
41+
do_generic_syscall(pid32, SYSCALL_CREATE, __VA_ARGS__)
3442

3543
/* in file ctxsw.S */
3644
extern void ctxsw(void *, void *);
@@ -71,6 +79,8 @@ extern char *getmem(uint32);
7179

7280
/* in file getpid.c */
7381
extern pid32 getpid(void);
82+
#define syscall_getpid(...) \
83+
do_argless_syscall(syscall, SYSCALL_GETPID)
7484

7585
/* in file getprio.c */
7686
extern syscall getprio(pid32);
@@ -79,7 +89,7 @@ extern syscall getprio(pid32);
7989
extern char *getstk(uint32);
8090

8191
/* in file getticks.c */
82-
extern uint64 getticks(void);
92+
extern void getticks(uint64 *);
8393

8494
/* in file gettime.c */
8595
extern status gettime(uint32 *);
@@ -117,6 +127,8 @@ extern devcall ionull(void);
117127

118128
/* in file kill.c */
119129
extern syscall kill(pid32);
130+
#define syscall_kill(...) \
131+
do_generic_syscall(syscall, SYSCALL_KILL, __VA_ARGS__)
120132

121133
/* in file lexan.c */
122134
extern int32 lexan(char *, int32, char *, int32 *, int32 [], int32 []);
@@ -241,6 +253,8 @@ extern qid16 newqueue(void);
241253

242254
/* in file open.c */
243255
extern syscall open(did32, char *, char *);
256+
#define syscall_open(...) \
257+
do_generic_syscall(syscall, SYSCALL_OPEN, __VA_ARGS__)
244258

245259
/* in file panic.c */
246260
extern void panic(char *);
@@ -297,15 +311,21 @@ extern devcall ramwrite(struct dentry *, char *, int32);
297311

298312
/* in file read.c */
299313
extern syscall read(did32, char *, uint32);
314+
#define syscall_read(...) \
315+
do_generic_syscall(pri16, SYSCALL_READ, __VA_ARGS__)
300316

301317
/* in file ready.c */
302318
extern status ready(pid32);
303319

304320
/* in file receive.c */
305321
extern umsg32 receive(void);
322+
#define syscall_receive() \
323+
do_argless_syscall(umsg32, SYSCALL_RECEIVE)
306324

307325
/* in file recvclr.c */
308326
extern umsg32 recvclr(void);
327+
#define syscall_recvclr() \
328+
do_argless_syscall(umsg32, SYSCALL_RECVCLR)
309329

310330
/* in file recvtime.c */
311331
extern umsg32 recvtime(int32);
@@ -319,6 +339,8 @@ extern void restore(intmask);
319339

320340
/* in file resume.c */
321341
extern pri16 resume(pid32);
342+
#define syscall_resume(...) \
343+
do_generic_syscall(pri16, SYSCALL_RESUME, __VA_ARGS__)
322344

323345
/* in file seek.c */
324346
extern syscall seek(did32, uint32);
@@ -351,6 +373,11 @@ extern syscall signaln(sid32, int32);
351373
extern syscall sleepms(int32);
352374
extern syscall sleep(int32);
353375

376+
#define syscall_sleepms(...) \
377+
do_generic_syscall(syscall, SYSCALL_SLEEPMS, __VA_ARGS__)
378+
#define syscall_sleep(...) \
379+
do_generic_syscall(syscall, SYSCALL_SLEEP, __VA_ARGS__)
380+
354381
/* in file start.S */
355382
extern int32 inb(int32);
356383
extern int32 inw(int32);

include/stdio.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* stdio.h - definintions and constants for standard I/O functions */
22

3+
#include "syscalls.h"
34

45
/* Prototypes for formatted input functions */
56

@@ -9,6 +10,9 @@ extern int32 sscanf(char *, char *, int32);
910
extern int32 fscanf(int32, char *, int32);
1011
#define scanf(fmt, args) fscanf(CONSOLE, fmt, args)
1112

13+
#define syscall_fscanf(...) \
14+
do_generic_syscall(int32, SYSCALL_FSCANF, __VA_ARGS__)
15+
#define syscall_scanf(...) syscall_fscanf(CONSOLE, __VA_ARGS__)
1216

1317
/* Definintion of standard input/ouput/error used with shell commands */
1418

@@ -23,6 +27,10 @@ extern int32 fprintf(int, char *, ...);
2327
extern int32 printf(const char *, ...);
2428
extern int32 sprintf(char *, char *, ...);
2529

30+
#define syscall_fprintf(...) \
31+
do_generic_syscall(int32, SYSCALL_FPRINTF, __VA_ARGS__)
32+
#define syscall_printf(...) \
33+
do_generic_syscall(int32, SYSCALL_PRINTF, __VA_ARGS__)
2634

2735
/* Prototypes for character input and output functions */
2836

include/syscalls.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include "valen.h"
4+
5+
#define do_argless_syscall(rettype, id) \
6+
((rettype)do_syscall(id, 0))
7+
#define do_generic_syscall(rettype, id, ...) \
8+
((rettype)do_syscall(id, va_args_len(__VA_ARGS__), __VA_ARGS__))
9+
10+
#define SYSCALL_CREATE 1
11+
#define SYSCALL_RESUME 2
12+
#define SYSCALL_RECVCLR 3
13+
#define SYSCALL_RECEIVE 4
14+
#define SYSCALL_SLEEPMS 5
15+
#define SYSCALL_SLEEP 6
16+
#define SYSCALL_FPRINTF 7
17+
#define SYSCALL_PRINTF 8
18+
#define SYSCALL_FSCANF 9
19+
#define SYSCALL_READ 10
20+
#define SYSCALL_OPEN 11
21+
#define SYSCALL_CONTROL 12
22+
#define SYSCALL_KILL 13
23+
#define SYSCALL_GETPID 14

include/valen.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
// Count a __VA_ARGS__
4+
// From: https://stackoverflow.com/questions/2124339/c-preprocessor-va-args-number-of-arguments
5+
#define PP_NARG(...) \
6+
PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
7+
#define PP_NARG_(...) \
8+
PP_ARG_N(__VA_ARGS__)
9+
#define PP_ARG_N( \
10+
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
11+
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
12+
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
13+
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
14+
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
15+
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
16+
_61,_62,_63,N,...) N
17+
#define PP_RSEQ_N() \
18+
63,62,61,60, \
19+
59,58,57,56,55,54,53,52,51,50, \
20+
49,48,47,46,45,44,43,42,41,40, \
21+
39,38,37,36,35,34,33,32,31,30, \
22+
29,28,27,26,25,24,23,22,21,20, \
23+
19,18,17,16,15,14,13,12,11,10, \
24+
9,8,7,6,5,4,3,2,1,0
25+
26+
#define va_args_len PP_NARG

shell/shell.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,23 @@ process shell (
8383

8484
/* Print shell banner and startup message */
8585

86-
fprintf(dev, "\n\n%s%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
86+
syscall_fprintf(dev, "\n\n%s%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
8787
SHELL_BAN0,SHELL_BAN1,SHELL_BAN2,SHELL_BAN3,SHELL_BAN4,
8888
SHELL_BAN5,SHELL_BAN6,SHELL_BAN7,SHELL_BAN8,SHELL_BAN9);
8989

90-
fprintf(dev, "%s\n\n", SHELL_STRTMSG);
90+
syscall_fprintf(dev, "%s\n\n", SHELL_STRTMSG);
9191

9292
/* Continually prompt the user, read input, and execute command */
9393

9494
while (TRUE) {
9595

9696
/* Display prompt */
9797

98-
fprintf(dev, SHELL_PROMPT);
98+
syscall_fprintf(dev, SHELL_PROMPT);
9999

100100
/* Read a command */
101101

102-
len = read(dev, buf, sizeof(buf));
102+
len = syscall_read(dev, buf, sizeof(buf));
103103

104104
/* Exit gracefully on end-of-file */
105105

@@ -122,14 +122,14 @@ process shell (
122122
/* Handle parsing error */
123123

124124
if (ntok == SYSERR) {
125-
fprintf(dev,"%s\n", SHELL_SYNERRMSG);
125+
syscall_fprintf(dev,"%s\n", SHELL_SYNERRMSG);
126126
continue;
127127
}
128128

129129
/* If line is empty, go to next input line */
130130

131131
if (ntok == 0) {
132-
fprintf(dev, "\n");
132+
syscall_fprintf(dev, "\n");
133133
continue;
134134
}
135135

@@ -150,7 +150,7 @@ process shell (
150150
if ( (ntok >=3) && ( (toktyp[ntok-2] == SH_TOK_LESS)
151151
||(toktyp[ntok-2] == SH_TOK_GREATER))){
152152
if (toktyp[ntok-1] != SH_TOK_OTHER) {
153-
fprintf(dev,"%s\n", SHELL_SYNERRMSG);
153+
syscall_fprintf(dev,"%s\n", SHELL_SYNERRMSG);
154154
continue;
155155
}
156156
if (toktyp[ntok-2] == SH_TOK_LESS) {
@@ -166,18 +166,18 @@ process shell (
166166
if ( (ntok >=3) && ( (toktyp[ntok-2] == SH_TOK_LESS)
167167
||(toktyp[ntok-2] == SH_TOK_GREATER))){
168168
if (toktyp[ntok-1] != SH_TOK_OTHER) {
169-
fprintf(dev,"%s\n", SHELL_SYNERRMSG);
169+
syscall_fprintf(dev,"%s\n", SHELL_SYNERRMSG);
170170
continue;
171171
}
172172
if (toktyp[ntok-2] == SH_TOK_LESS) {
173173
if (inname != NULL) {
174-
fprintf(dev,"%s\n", SHELL_SYNERRMSG);
174+
syscall_fprintf(dev,"%s\n", SHELL_SYNERRMSG);
175175
continue;
176176
}
177177
inname = &tokbuf[tok[ntok-1]];
178178
} else {
179179
if (outname != NULL) {
180-
fprintf(dev,"%s\n", SHELL_SYNERRMSG);
180+
syscall_fprintf(dev,"%s\n", SHELL_SYNERRMSG);
181181
continue;
182182
}
183183
outname = &tokbuf[tok[ntok-1]];
@@ -194,7 +194,7 @@ process shell (
194194
}
195195
}
196196
if ((ntok == 0) || (i < ntok)) {
197-
fprintf(dev, SHELL_SYNERRMSG);
197+
syscall_fprintf(dev, SHELL_SYNERRMSG);
198198
continue;
199199
}
200200

@@ -224,15 +224,15 @@ process shell (
224224
/* Handle command not found */
225225

226226
if (j >= ncmd) {
227-
fprintf(dev, "command %s not found\n", tokbuf);
227+
syscall_fprintf(dev, "command %s not found\n", tokbuf);
228228
continue;
229229
}
230230

231231
/* Handle built-in command */
232232

233233
if (cmdtab[j].cbuiltin) { /* No background or redirect. */
234234
if (inname != NULL || outname != NULL || backgnd){
235-
fprintf(dev, SHELL_BGERRMSG);
235+
syscall_fprintf(dev, SHELL_BGERRMSG);
236236
continue;
237237
} else {
238238
/* Set up arg vector for call */
@@ -254,25 +254,25 @@ process shell (
254254
/* Open files and redirect I/O if specified */
255255

256256
if (inname != NULL) {
257-
stdinput = open(NAMESPACE,inname,"ro");
257+
stdinput = syscall_open(NAMESPACE,inname,"ro");
258258
if (stdinput == SYSERR) {
259-
fprintf(dev, SHELL_INERRMSG, inname);
259+
syscall_fprintf(dev, SHELL_INERRMSG, inname);
260260
continue;
261261
}
262262
}
263263
if (outname != NULL) {
264-
stdoutput = open(NAMESPACE,outname,"w");
264+
stdoutput = syscall_open(NAMESPACE,outname,"w");
265265
if (stdoutput == SYSERR) {
266-
fprintf(dev, SHELL_OUTERRMSG, outname);
266+
syscall_fprintf(dev, SHELL_OUTERRMSG, outname);
267267
continue;
268268
} else {
269-
control(stdoutput, F_CTL_TRUNC, 0, 0);
269+
syscall_control(stdoutput, F_CTL_TRUNC, 0, 0);
270270
}
271271
}
272272

273273
/* Spawn child thread for non-built-in commands */
274274

275-
child = create(cmdtab[j].cfunc,
275+
child = syscall_create(cmdtab[j].cfunc,
276276
SHELL_CMDSTK, SHELL_CMDPRIO,
277277
cmdtab[j].cname, 2, ntok, &tmparg);
278278

@@ -281,7 +281,7 @@ process shell (
281281
if ((child == SYSERR) ||
282282
(addargs(child, ntok, tok, tlen, tokbuf, &tmparg)
283283
== SYSERR) ) {
284-
fprintf(dev, SHELL_CREATMSG);
284+
syscall_fprintf(dev, SHELL_CREATMSG);
285285
continue;
286286
}
287287

@@ -290,18 +290,18 @@ process shell (
290290
proctab[child].prdesc[0] = stdinput;
291291
proctab[child].prdesc[1] = stdoutput;
292292

293-
msg = recvclr();
294-
resume(child);
293+
msg = syscall_recvclr();
294+
syscall_resume(child);
295295
if (! backgnd) {
296-
msg = receive();
296+
msg = syscall_receive();
297297
while (msg != child) {
298-
msg = receive();
298+
msg = syscall_receive();
299299
}
300300
}
301301
}
302302

303303
/* Terminate the shell process by returning from the top level */
304304

305-
fprintf(dev,SHELL_EXITMSG);
305+
syscall_fprintf(dev,SHELL_EXITMSG);
306306
return OK;
307307
}

0 commit comments

Comments
 (0)