aboutsummaryrefslogtreecommitdiff
path: root/acid/syscall
blob: 754b4449a774f4a3c46f792fa6294a4c9c804a5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// print system calls
defn printstring(s)
{
	print("\"", s, "\"");
}

defn printtextordata(addr, n)
{
	local a, i;

	a = addr\c;
	i = 0;
	loop 1, n do {
		if (a[i]>=127) then {
			print(fmt(addr, 'X'), ", ", n\D);
			return {};
		}
		i = i+1;
	}

	print("\"");
	printstringn(addr, n);
	print("\"");
}

defn printstringn(s, n)
{
	local m;

	m = n;
	if (m > 100) then m = 100;
	loop 1,m do {
		print(*(s\c)); s=s+1;
	}
	if(m != n) then print("...");
}

defn printsyscall(name, fmt, arg) {
	local f, i, a, argp, sl;

	print(name, "(");
	i = 0;
	a = eval arg;
	while fmt[i] != 0 do {
		if fmt[i] == 's' then {
			if *a == 0 then
				print("nil");
			else
				printstring(*(*a\s));
		} else if fmt[i] == 'S' then {
			argp = *a;
			argl = {};
			while *argp != 0 do {
				argl = append argl, *(*argp\s);
				argp++;
			}
			print(argl);
		} else if (fmt[i] == 'Z') && (~*a == 0) then {
			print("-1");
			a++;	// advance extra word for quadword
		} else if (fmt[i] == 'Y') || (fmt[i] == 'V') then {
			print(fmt(*a, fmt[i]));
			a++;	// advance extra word for quadword
		} else if (fmt[i] == 'T') then {
			if *a == 0 then
				print("nil");
			else
				printtextordata(*a, a[1]);
		} else
			print(fmt(*a, fmt[i]));
		if fmt[i+1] != 0 then
			print(", ");
		i = i+1;
		a++;
	}
	print(")\n");
}

defn code(*e) { return e; }

syscalls = {
	{ 0, {"sysr1",		"s",		code(0)}},
	{ 1, {"_errstr",		"s",		code(*sys_errstr:arg)}},
	{ 2, {"bind",		"ssX",		code(*sysbind:arg)}},
	{ 3, {"chdir",		"s",		code(*sysbind:arg)}},
	{ 4, {"close",		"D",		code(*sysclose:arg)}},
	{ 5, {"dup",		"DD",		code(*sysdup:arg)}},
	{ 6, {"alarm",		"D",		code(*sysalarm:arg)}},
	{ 7, {"exec",		"sS",		code(*sysexec:arg)}},
	{ 8, {"exits",		"s",		code(*sysexits:arg)}},
	{ 9, {"_fsession",	"DX",		code(*sys_fsession:arg)}},
	{10, {"fauth",		"DX",		code(*sysfauth:arg)}},
	{11, {"_fstat",		"DX",		code(*sys_fstat:arg)}},
	{12, {"segbrk",		"XX",		code(*syssegbrk:arg)}},
	{13, {"_mount",		"DsXs",		code(*sys_mount:arg)}},
	{14, {"open",		"sD",		code(*sysopen:arg)}},
	{15, {"_read",		"DXD",		code(*sys_read:arg)}},
	{16, {"oseek",		"DDD",		code(*sysoseek:arg)}},
	{17, {"sleep",		"D",		code(*syssleep:arg)}},
	{18, {"_stat",		"sX",		code(*sys_stat:arg)}},
	{19, {"rfork",		"X",		code(*sysstat:arg)}},
	{20, {"_write",		"DXD",		code(*sys_write:arg)}},
	{21, {"pipe",		"X",		code(*syspipe:arg)}},
	{22, {"create",		"sDO",		code(*syscreate:arg)}},
	{23, {"fd2path",	"DXD",		code(*sysfd2path:arg)}},
	{24, {"brk_",		"X",		code(*sysbrk_:arg)}},
	{25, {"remove",		"s",		code(*sysremove:arg)}},
	{26, {"_wstat",		"sX",		code(*sys_wstat:arg)}},
	{27, {"_fwstat",		"DX",		code(*sys_fwstat:arg)}},
	{28, {"notify",		"X",		code(*sysnotify:arg)}},
	{29, {"noted",		"D",		code(*sysnoted:arg)}},
	{30, {"segattach",	"DsXD",		code(*syssegattach:arg)}},
	{31, {"segdetach",	"X",		code(*syssegdetach:arg)}},
	{32, {"segfree",	"XD",		code(*syssegfree:arg)}},
	{33, {"segflush",	"XD",		code(*syssegflush:arg)}},
	{34, {"rendezvous",	"XX",		code(*sysrendezvous:arg)}},
	{35, {"unmount",	"ss",		code(*sysunmount:arg)}},
	{36, {"_wait",		"X",		code(*sys_wait:arg)}},
	{39, {"seek",		"XDVD",		code(*sysseek:arg)}},
	{40, {"fversion",	"DDsD",		code(*sysfversion:arg)}},
	{41, {"errstr",		"TD",		code(*syserrstr:arg)}},
	{42, {"stat",		"sXD",		code(*sysstat:arg)}},
	{43, {"fstat",		"DXD",		code(*sysfstat:arg)}},
	{44, {"wstat",		"sXD",		code(*syswstat:arg)}},
	{45, {"fwstat",		"DXD",		code(*sysfwstat:arg)}},
	{46, {"mount",		"DDsXs",		code(*sysmount:arg)}},
	{47, {"await",		"TD",		code(*sysawait:arg)}},
	{50, {"pread",		"DXDZ",		code(*syspread:arg)}},
	{51, {"pwrite",		"DTDZ",		code(*syspwrite:arg)}},
};

defn syscall() {
	local n, sl, h, p;

	map({"*data", 0, 0xffffffff, 0});
	n = *syscall:scallnr;
	sl = syscalls;
	while sl != {} do {
		h = head sl;
		sl = tail sl;

		if n == h[0] then {
			p = h[1];
			printsyscall(p[0], p[1], p[2]);
		}
	}
}

defn UPCSPRET() {
	// return sys call number, address of first argument, location of syscall return value
	if objtype == "386" then 
		return { code(*(*PC-4)), code(*SP+4), code(*AX) };
	if (objtype == "mips") || (objtype == "mips2") then
		return { code(*(*PC-4) & 0xffff), code(*SP+4), code(*R1) };
	if objtype == "arm" then
		return { code(*(*PC-4) & 0xffff), code(*SP+4), code(*R0) };	// untested
	if objtype == "alpha" then
		return { code(*(*PC-4) & 0xffff), code(*SP+4), code(*R0) };	// untested
}

defn trapoffset() {
	// return offset from entry point to trap instr
	if objtype == "386" then return 5;
	if objtype == "mips" then return 8;
	if objtype == "mips2" then return 8;
	if objtype == "arm" then return 8;	// untested
	if objtype == "alpha" then return 8;	// untested
}	

defn trapreason() {
	// return reason for trap
	if objtype == "386" then return reason(*TRAP);
	if objtype == "mips" then return reason(*CAUSE);
	if objtype == "mips2" then return reason(*CAUSE);
	if objtype == "arm" then return "unknown trap";	// untested
	if objtype == "alpha" then return reason(cause);	// untested
}


defn usyscall() {	// gives args for system call in user level; not useful with -k
	local n, sl, h, p;

	// stopped at TRAP instruction in system call library
	pcsp = UPCSPRET();
	n = eval pcsp[0];
	sl = syscalls;
	while sl != {} do {
		h = head sl;
		sl = tail sl;

		if n == h[0] then {
			p = h[1];
			printsyscall(p[0], p[1], pcsp[1]);
		}
	}
}