summaryrefslogtreecommitdiff
path: root/sys/src/cmd/5e/proc.c
blob: 4114202822866719d19fb43ad069515d9a1d7f28 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <bio.h>
#include <mach.h>
#include <ctype.h>
#include <tos.h>
#include "dat.h"
#include "fns.h"

void
initproc(void)
{
	P = emallocz(sizeof(Process));
	P->pid = getpid();
	P->fd = newfd();
}

static void
initstack(int argc, char **argv)
{
	ulong tos, sp, ap, size, i, len;
	
	tos = STACKTOP - sizeof(Tos) * 2;
	sp = tos;
	
	size = 8;
	for(i = 0; i < argc; i++)
		size += strlen(argv[i]) + 5;
	
	sp -= size;
	sp &= ~7;
	P->R[0] = tos;
	P->R[1] = STACKTOP - 4;
	P->R[13] = sp;
	
	*(ulong *) vaddrnol(sp) = argc;
	sp += 4;
	ap = sp + (argc + 1) * 4;
	for(i = 0; i < argc; i++) {
		*(ulong *) vaddrnol(sp) = ap;
		sp += 4;
		len = strlen(argv[i]) + 1;
		memcpy(vaddrnol(ap), argv[i], len);
		ap += len;
	}
	*(ulong *) vaddrnol(sp) = 0;

	((Tos *) vaddrnol(tos))->pid = getpid();
}

static int
loadscript(int fd, char *file, int argc, char **argv)
{
	char buf[513], *p, **q, **nargv;
	int rc, nargc, i;
	
	seek(fd, 0, 0);
	rc = readn(fd, buf, 512);
	if(rc <= 0)
		goto invalid;
	close(fd);
	buf[rc] = 0;
	p = strchr(buf, '\n');
	if(p == nil)
		goto invalid;
	*p = 0;
	while(isspace(*--p))
		*p = 0;
	nargc = 0;
	p = buf + 2;
	while(*p) {
		while(*p && isspace(*p))
			p++;
		nargc++;
		while(*p && !isspace(*p))
			p++;
	}
	if(nargc == 0)
		goto invalid;
	nargv = emallocz(sizeof(char *) * (nargc + argc));
	q = nargv;
	p = buf + 2;
	while(*p) {
		while(*p && isspace(*p))
			p++;
		*(p-1) = 0;
		*q++ = p;
		while(*p && !isspace(*p))
			p++;
	}
	*q++ = file;
	for(i = 1; i < argc; i++)
		*q++ = argv[i];
	rc = loadtext(*nargv, argc + nargc, nargv);
	free(nargv);
	return rc;

invalid:
	werrstr("exec header invalid");
	return -1;
}

int
loadtext(char *file, int argc, char **argv)
{
	int fd, i;
	Fhdr fp;
	Segment *text, *data, *bss, *stack;
	char buf[2];
	
	fd = open(file, OREAD);
	if(fd < 0) return -1;
	if(pread(fd, buf, 2, 0) == 2 && buf[0] == '#' && buf[1] == '!')
		return loadscript(fd, file, argc, argv);
	seek(fd, 0, 0);
	if(crackhdr(fd, &fp) == 0) {
		werrstr("exec header invalid");
		return -1;
	}
	if(fp.magic != E_MAGIC) {
		werrstr("exec header invalid");
		return -1;
	}
	freesegs();
	memset(P->R, 0, sizeof(P->R));
	P->CPSR = 0;
	text = newseg(fp.txtaddr - fp.hdrsz, fp.txtsz + fp.hdrsz, SEGTEXT);
	data = newseg(fp.dataddr, fp.datsz, SEGDATA);
	bss = newseg(fp.dataddr + fp.datsz, fp.bsssz, SEGBSS);
	stack = newseg(STACKTOP - STACKSIZE, STACKSIZE, SEGSTACK);
	seek(fd, fp.txtoff - fp.hdrsz, 0);
	if(readn(fd, text->data, fp.txtsz + fp.hdrsz) < fp.txtsz + fp.hdrsz)
		sysfatal("%r");
	seek(fd, fp.datoff, 0);
	if(readn(fd, data->data, fp.datsz) < fp.datsz)
		sysfatal("%r");
	memset(bss->data, 0, bss->size);
	memset(stack->data, 0, stack->size);
	P->R[15] = fp.entry;
	if(havesymbols && syminit(fd, &fp) < 0)
		fprint(2, "initializing symbol table: %r\n");
	close(fd);
	for(i = 0; i < P->fd->nfds * 8; i++)
		if(iscexec(P->fd, i))
			close(i);
	wlock(P->fd);
	free(P->fd->fds);
	P->fd->fds = nil;
	P->fd->nfds = 0;
	wunlock(P->fd);
	initstack(argc, argv);
	return 0;
}

void
cherrstr(char *str, ...)
{
	va_list va;
	
	va_start(va, str);
	vsnprint(P->errbuf, ERRMAX, str, va);
	va_end(va);
}

u32int
noteerr(u32int x, u32int y)
{
	if(((int)x) >= ((int)y))
		return x;
	rerrstr(P->errbuf, ERRMAX);
	return x;
}

Fd *
newfd(void)
{
	Fd *fd;
	
	fd = emallocz(sizeof(*fd));
	incref(&fd->ref);
	return fd;
}

Fd *
copyfd(Fd *old)
{
	Fd *new;
	
	rlock(old);
	new = newfd();
	if(old->nfds > 0) {
		new->nfds = old->nfds;
		new->fds = emalloc(old->nfds);
		memcpy(new->fds, old->fds, old->nfds);
	}
	runlock(old);
	return new;
}

void
fddecref(Fd *fd)
{
	if(decref(&fd->ref) == 0) {
		free(fd->fds);
		free(fd);
	}
}

int
iscexec(Fd *fd, int n)
{
	int r;

	r = 0;
	rlock(fd);
	if(n / 8 < fd->nfds)
		r = (fd->fds[n / 8] & (1 << (n % 8))) != 0;
	runlock(fd);
	return r;
}

void
setcexec(Fd *fd, int n, int status)
{
	int old;

	wlock(fd);
	if(n / 8 >= fd->nfds) {
		if(status == 0) {
			wunlock(fd);
			return;
		}
		old = fd->nfds;
		fd->nfds = (n / 8) + 1;
		fd->fds = erealloc(fd->fds, fd->nfds);
		memset(fd->fds + old, 0, fd->nfds - old);
	}
	if(status == 0)
		fd->fds[n / 8] &= ~(1 << (n % 8));
	else
		fd->fds[n / 8] |= (1 << (n % 8));
	wunlock(fd);
}