CS318 - Pintos
Pintos source browser for JHU CS318 course
syscall.c
Go to the documentation of this file.
1 #include <syscall.h>
2 #include "../syscall-nr.h"
3 
4 /** Invokes syscall NUMBER, passing no arguments, and returns the
5  return value as an `int'. */
6 #define syscall0(NUMBER) \
7  ({ \
8  int retval; \
9  asm volatile \
10  ("pushl %[number]; int $0x30; addl $4, %%esp" \
11  : "=a" (retval) \
12  : [number] "i" (NUMBER) \
13  : "memory"); \
14  retval; \
15  })
16 
17 /** Invokes syscall NUMBER, passing argument ARG0, and returns the
18  return value as an `int'. */
19 #define syscall1(NUMBER, ARG0) \
20  ({ \
21  int retval; \
22  asm volatile \
23  ("pushl %[arg0]; pushl %[number]; int $0x30; addl $8, %%esp" \
24  : "=a" (retval) \
25  : [number] "i" (NUMBER), \
26  [arg0] "g" (ARG0) \
27  : "memory"); \
28  retval; \
29  })
30 
31 /** Invokes syscall NUMBER, passing arguments ARG0 and ARG1, and
32  returns the return value as an `int'. */
33 #define syscall2(NUMBER, ARG0, ARG1) \
34  ({ \
35  int retval; \
36  asm volatile \
37  ("pushl %[arg1]; pushl %[arg0]; " \
38  "pushl %[number]; int $0x30; addl $12, %%esp" \
39  : "=a" (retval) \
40  : [number] "i" (NUMBER), \
41  [arg0] "r" (ARG0), \
42  [arg1] "r" (ARG1) \
43  : "memory"); \
44  retval; \
45  })
46 
47 /** Invokes syscall NUMBER, passing arguments ARG0, ARG1, and
48  ARG2, and returns the return value as an `int'. */
49 #define syscall3(NUMBER, ARG0, ARG1, ARG2) \
50  ({ \
51  int retval; \
52  asm volatile \
53  ("pushl %[arg2]; pushl %[arg1]; pushl %[arg0]; " \
54  "pushl %[number]; int $0x30; addl $16, %%esp" \
55  : "=a" (retval) \
56  : [number] "i" (NUMBER), \
57  [arg0] "r" (ARG0), \
58  [arg1] "r" (ARG1), \
59  [arg2] "r" (ARG2) \
60  : "memory"); \
61  retval; \
62  })
63 
64 void
65 halt (void)
66 {
68  NOT_REACHED ();
69 }
70 
71 void
72 exit (int status)
73 {
74  syscall1 (SYS_EXIT, status);
75  NOT_REACHED ();
76 }
77 
78 pid_t
79 exec (const char *file)
80 {
81  return (pid_t) syscall1 (SYS_EXEC, file);
82 }
83 
84 int
85 wait (pid_t pid)
86 {
87  return syscall1 (SYS_WAIT, pid);
88 }
89 
90 bool
91 create (const char *file, unsigned initial_size)
92 {
93  return syscall2 (SYS_CREATE, file, initial_size);
94 }
95 
96 bool
97 remove (const char *file)
98 {
99  return syscall1 (SYS_REMOVE, file);
100 }
101 
102 int
103 open (const char *file)
104 {
105  return syscall1 (SYS_OPEN, file);
106 }
107 
108 int
109 filesize (int fd)
110 {
111  return syscall1 (SYS_FILESIZE, fd);
112 }
113 
114 int
115 read (int fd, void *buffer, unsigned size)
116 {
117  return syscall3 (SYS_READ, fd, buffer, size);
118 }
119 
120 int
121 write (int fd, const void *buffer, unsigned size)
122 {
123  return syscall3 (SYS_WRITE, fd, buffer, size);
124 }
125 
126 void
127 seek (int fd, unsigned position)
128 {
129  syscall2 (SYS_SEEK, fd, position);
130 }
131 
132 unsigned
133 tell (int fd)
134 {
135  return syscall1 (SYS_TELL, fd);
136 }
137 
138 void
139 close (int fd)
140 {
141  syscall1 (SYS_CLOSE, fd);
142 }
143 
144 mapid_t
145 mmap (int fd, void *addr)
146 {
147  return syscall2 (SYS_MMAP, fd, addr);
148 }
149 
150 void
152 {
153  syscall1 (SYS_MUNMAP, mapid);
154 }
155 
156 bool
157 chdir (const char *dir)
158 {
159  return syscall1 (SYS_CHDIR, dir);
160 }
161 
162 bool
163 mkdir (const char *dir)
164 {
165  return syscall1 (SYS_MKDIR, dir);
166 }
167 
168 bool
169 readdir (int fd, char name[READDIR_MAX_LEN + 1])
170 {
171  return syscall2 (SYS_READDIR, fd, name);
172 }
173 
174 bool
175 isdir (int fd)
176 {
177  return syscall1 (SYS_ISDIR, fd);
178 }
179 
180 int
181 inumber (int fd)
182 {
183  return syscall1 (SYS_INUMBER, fd);
184 }
name
char * name[]
Definition: insult.c:47
SYS_READDIR
Reads a directory entry.
Definition: syscall-nr.h:29
SYS_ISDIR
Tests if a fd represents a directory.
Definition: syscall-nr.h:30
SYS_CHDIR
Change the current directory.
Definition: syscall-nr.h:27
readdir
bool readdir(int fd, char name[READDIR_MAX_LEN+1])
Definition: syscall.c:169
SYS_MKDIR
Create a directory.
Definition: syscall-nr.h:28
SYS_TELL
Report current position in a file.
Definition: syscall-nr.h:19
write
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
SYS_FILESIZE
Obtain a file's size.
Definition: syscall-nr.h:15
SYS_EXEC
Start another process.
Definition: syscall-nr.h:10
file
An open file.
Definition: file.c:7
SYS_OPEN
Open a file.
Definition: syscall-nr.h:14
SYS_CREATE
Create a file.
Definition: syscall-nr.h:12
SYS_REMOVE
Delete a file.
Definition: syscall-nr.h:13
exec
pid_t exec(const char *file)
Definition: syscall.c:79
SYS_WAIT
Wait for a child process to die.
Definition: syscall-nr.h:11
SYS_INUMBER
Returns the inode number for a fd.
Definition: syscall-nr.h:31
NOT_REACHED
#define NOT_REACHED()
lib/debug.h
Definition: debug.h:35
syscall1
#define syscall1(NUMBER, ARG0)
Invokes syscall NUMBER, passing argument ARG0, and returns the return value as an ‘int’.
Definition: syscall.c:19
buffer
static struct intq buffer
Stores keys from the keyboard and serial port.
Definition: input.c:7
mkdir
bool mkdir(const char *dir)
Definition: syscall.c:163
remove
bool remove(const char *file)
Definition: syscall.c:97
chdir
bool chdir(const char *dir)
Project 4 only.
Definition: syscall.c:157
open
int open(const char *file)
Definition: syscall.c:103
SYS_MUNMAP
Remove a memory mapping.
Definition: syscall-nr.h:24
SYS_HALT
Halt the operating system.
Definition: syscall-nr.h:8
tell
unsigned tell(int fd)
Definition: syscall.c:133
halt
void halt(void)
Projects 2 and later.
Definition: syscall.c:65
syscall3
#define syscall3(NUMBER, ARG0, ARG1, ARG2)
Invokes syscall NUMBER, passing arguments ARG0, ARG1, and ARG2, and returns the return value as an ‘i...
Definition: syscall.c:49
SYS_MMAP
Map a file into memory.
Definition: syscall-nr.h:23
SYS_SEEK
Change position in a file.
Definition: syscall-nr.h:18
seek
void seek(int fd, unsigned position)
Definition: syscall.c:127
filesize
int filesize(int fd)
Definition: syscall.c:109
inumber
int inumber(int fd)
lib/user/syscall.h
Definition: syscall.c:181
isdir
bool isdir(int fd)
Definition: syscall.c:175
READDIR_MAX_LEN
#define READDIR_MAX_LEN
Maximum characters in a filename written by readdir().
Definition: syscall.h:16
pid_t
int pid_t
Process identifier.
Definition: syscall.h:8
close
void close(int fd)
Definition: syscall.c:139
syscall2
#define syscall2(NUMBER, ARG0, ARG1)
Invokes syscall NUMBER, passing arguments ARG0 and ARG1, and returns the return value as an ‘int’.
Definition: syscall.c:33
SYS_CLOSE
Close a file.
Definition: syscall-nr.h:20
mmap
mapid_t mmap(int fd, void *addr)
Project 3 and optionally project 4.
Definition: syscall.c:145
SYS_EXIT
Terminate this process.
Definition: syscall-nr.h:9
wait
int wait(pid_t pid)
Definition: syscall.c:85
exit
void exit(int status)
Definition: syscall.c:72
syscall0
#define syscall0(NUMBER)
Invokes syscall NUMBER, passing no arguments, and returns the return value as an ‘int’.
Definition: syscall.c:6
SYS_WRITE
Write to a file.
Definition: syscall-nr.h:17
munmap
void munmap(mapid_t mapid)
Definition: syscall.c:151
mapid_t
int mapid_t
Map region identifier.
Definition: syscall.h:12
dir
A directory.
Definition: directory.c:10
read
int read(int fd, void *buffer, unsigned size)
Definition: syscall.c:115
create
bool create(const char *file, unsigned initial_size)
Definition: syscall.c:91
syscall.h
SYS_READ
Read from a file.
Definition: syscall-nr.h:16