summaryrefslogtreecommitdiff
path: root/emulators/ski/patches/patch-an
blob: 19a36bb5dac92e23950e7dbad4e5c7c7dc668519 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
$NetBSD: patch-an,v 1.3 2019/11/17 23:05:19 scole Exp $

--- src/netbsd/ssc-netbsd.c.orig	2010-11-04 05:01:51.000000000 +0000
+++ src/netbsd/ssc-netbsd.c	2016-05-06 14:44:42.000000000 -0400
@@ -0,0 +1,464 @@
+/*
+ * Copyright (C) 1995-2007, Hewlett-Packard Development Company, L.P.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or 
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <assert.h>
+#include <fcntl.h>
+#include <util.h>
+#include <poll.h>
+#include <signal.h>
+#include <string.h>
+#include <termios.h>
+#include <unistd.h>
+
+/* XXX - B0 is defined in termios.h to mean 0 baud. B0 is also
+ *	 a macro defined in field.h. Undefine B0 here...
+ */
+#undef B0
+
+#include "std.h"
+#include "types.h"
+#include "bits.h"
+#include "exportui.h"
+#include "coreui.h"
+#include "fields.h"
+#include "libui.h"
+#include "sim.h"
+#include "state.h"
+#include "simmem.h"
+#include "ssc.h"
+
+#include "interruption.h"
+
+#include "netbsd/machdep-netbsd.h"
+
+extern char commandline[];
+extern unsigned int commandline_len;
+
+#ifdef NEW_MP
+extern REG os_boot_rendez_ip;
+extern REG os_boot_rendez_gp;
+#endif
+
+SscReqNode *sscHead = NULL;
+SscReqNode *sscPend = NULL;
+
+static struct {
+	short	type;
+	short	irrBit;
+} irptTbl[30];
+static unsigned int irptTop = 0;
+
+static pid_t console;
+static int fdin, fdout;
+static struct termios saved_tios;
+
+static void sigchld_handler(int sig, siginfo_t *si, void *ctx)
+{
+	tcsetattr(fdin, TCSANOW, &saved_tios);
+	_exit(1);
+}
+
+static void restore_tios(void)
+{
+	int status;
+
+	tcsetattr(fdin, TCSANOW, &saved_tios);
+	if (console) {
+		kill(console, SIGKILL);
+		wait(&status);
+	}
+}
+
+static void init_console(int fin, int fout)
+{
+	struct termios tios;
+
+	tcgetattr(fin, &tios);
+	saved_tios = tios;
+	tios.c_lflag &= ~(ICANON|ECHO);
+	tios.c_iflag &= ~(ICRNL);	/* do not map CR to NL on input */
+	tcsetattr(fin, TCSANOW, &tios);
+	atexit(restore_tios);
+	fdin = fin;
+	fdout = fout;
+}
+
+static int xconsole()
+{
+	char name[PATH_MAX];
+	struct sigaction act;
+	char tty[PATH_MAX];
+	int namelen;
+	int master, slave;
+
+	if (openpty(&master, &slave, name, NULL, NULL) == -1)
+		goto fail;
+
+	console = fork();
+	switch (console) {
+	case -1:
+		goto fail;
+	case 0: /* child */
+		close(0);
+		close(1);
+		close(2);
+		close(master);
+
+		sprintf(tty, "-S%s/%d", name, slave); 
+		execlp("xterm", "xterm", tty, "-geo", "80x24", "-name",
+		    "xski console", (char *)NULL);
+		perror("execlp(\"xterm\", ...) failed.");
+		abort();
+
+	default: /* parent */
+		close(slave);
+		init_console(master, master);
+		act.sa_sigaction = sigchld_handler;
+		act.sa_flags = SA_NOCLDSTOP|SA_SIGINFO;
+		sigaction(SIGCHLD, &act, NULL);
+
+		/* Read the window ID. */
+		if (read(master, name, 128) == -1)
+			goto fail;
+
+		fcntl(master, F_SETFL, O_NONBLOCK);
+
+		break;
+	}
+
+	return 0;
+
+ fail:
+	progExit("Unable to create console window\n");
+	return -1;
+}
+
+static BOOL addSscReq(int fd, size_t size)
+{
+	SscReqNode *np, *p;
+	int prevcnt = 0;
+
+	if (!(np = malloc(sizeof *np)))
+		return NO;
+	for (p = sscPend; p; p = p->next)
+		prevcnt += p->irptcnt;
+	np->fd = fd;
+	np->count = size;
+	np->irptcnt = IO_IRPT_CNT - prevcnt;
+	np->next = 0;
+	if (sscHead) {
+		for (p = sscHead; p->next; p = p->next) ;
+		p->next = np;
+	} else
+		sscHead = np;
+
+	if (!sscPend)
+		sscPend = np;
+
+	return YES;
+}
+
+static void delSscReq(SscReqNode *p)
+{
+	SscReqNode *q = sscHead;
+
+	if (p != sscHead) {
+		while (q->next != p)
+			q = q->next;
+		q->next = p->next;
+	} else
+		sscHead = sscHead->next;
+
+	if (p == sscPend)
+		sscPend = sscPend->next;
+	free(p);
+}
+
+void pendSscIrpt(unsigned int type)
+{
+	unsigned i, reg, bit, vector;
+	REG *p = &IRR0;			/* assume IRR0-3 are contiguous */
+
+	for (i = 0; i < irptTop; i++) {
+		if (type == irptTbl[i].type) {
+			vector = irptTbl[i].irrBit;
+			if (vector == 1 || (vector >= 3 && vector <= 15))
+				continue;	/* ignore vectors 1 and 3-15 */
+
+			reg = vector >> 6 & 0x3;
+			bit = vector & 0x3F;
+			*(p + reg) |= (REG)1 << bit;
+		}
+	}
+
+	if (acceptIrpt())
+		intrsim = extint = YES;
+}
+
+void doSSC(HWORD num, REG arg0, REG arg1, REG arg2, REG arg3, REG *ret)
+{
+	BOOL olddt = PSR_DT;
+
+	PSR_DT = 0;
+
+	switch (num) {
+	case SSC_CLOSE:
+		*ret = close(arg0);
+		break;
+
+	case SSC_CONSOLE_INIT: {
+		if (interface == BATCH)
+			init_console(0, 1);
+		else
+			xconsole();
+		break;
+	}
+
+	case SSC_EXIT:
+		GrWrtx (8, arg0, 0);
+		progExit("kernel exited with status %d\n", (int)arg0);
+		break;
+
+	case SSC_GENERATE_INTERRUPT:
+		pendSscIrpt(arg0);
+		break;
+
+	case SSC_GET_ARGS:
+		memBBWrt(arg0, commandline, commandline_len);
+		*ret = commandline_len;
+		break;
+
+	case SSC_GET_RTC: {
+		struct timeval tp;
+		struct timezone tzp;
+		struct tm *tm;
+		SscTime sscTime;
+
+		gettimeofday (&tp, &tzp);
+		tm = localtime((const time_t *)&tp.tv_sec);
+		sscTime.year = tm->tm_year;
+		sscTime.mon  = tm->tm_mon;
+		sscTime.mday = tm->tm_mday;
+		sscTime.hour = tm->tm_hour;
+		sscTime.min  = tm->tm_min;
+		sscTime.sec  = tm->tm_sec;
+		sscTime.msec = tp.tv_usec/1000;
+		sscTime.wday = tm->tm_wday;
+		memBBWrtP(arg0, (BYTE *)&sscTime, sizeof(SscTime));
+		/* no return value */
+		break;
+	}
+
+	case SSC_GET_TOD: {
+		struct timeval tp;
+		struct timezone tzp;
+ 		SscTimeval tv;
+		SscTimezone tz;
+
+		gettimeofday (&tp, &tzp);
+ 		tv.ssc_Sec = tp.tv_sec;
+		tv.ssc_Usec = tp.tv_usec;
+		tz.ssc_Minuteswest = tzp.tz_minuteswest;
+		tz.ssc_Dsttime = tzp.tz_dsttime;
+		memBBWrtP(arg0, (BYTE *)&tv, sizeof(tv));
+		if (arg1 != 0)
+			memBBWrtP(arg1, (BYTE *)&tz, sizeof(tz));
+		break;
+	}
+
+	case SSC_GETCHAR: {
+		int sz;
+		char buf[1];
+
+		sz = read(fdin, buf, 1);
+		*ret = (sz == 1) ? buf[0] : 0;
+		break;
+	}
+
+	case SSC_LOAD_SYMBOLS: {
+		char fn[PATH_MAX];
+		memBBRd(arg1, fn, 0);
+		*ret = (elfSymLoad(fn)) ? 0 : -1;
+		break;
+	}
+
+	case SSC_OPEN: {
+		int flags;
+		char fn[PATH_MAX];
+
+		memBBRd(arg0, fn, 0);
+		if (arg1 == SSC_READ_ACCESS)
+			flags = O_RDONLY;
+		else if (arg1 == SSC_WRITE_ACCESS)
+			flags = O_WRONLY;
+		else if (arg1 == (SSC_READ_ACCESS | SSC_WRITE_ACCESS))
+			flags = O_RDWR;
+		else
+			flags = -1;
+
+		*ret = (flags != -1) ? open(fn, flags) : -1;
+		break;
+	}
+
+	case SSC_PUTCHAR: {
+		char buf[1];
+
+		buf[0] = arg0;
+		write(fdout, buf, 1);
+		break;
+	}
+
+	case SSC_READ: {
+		SscDiskReq iov;
+		off_t filesize;
+		size_t size, ofs;
+		ADDR adr;
+		char *map;
+		int i;
+
+		filesize = lseek(arg0, 0LL, SEEK_END);
+		if (filesize == -1 || arg3 > filesize) {
+			*ret = -1;
+			break;
+		}
+
+		size = 0;
+		adr = arg2;
+		for (i = 0; i < arg1; i++) {
+			memBBRd(adr, (BYTE *)&iov, sizeof(iov));
+			size += iov.len;
+			adr += sizeof(iov);
+		}
+
+		if (arg3 + size > filesize)
+			size = filesize - arg3;
+
+		map = mmap(0, size, PROT_READ, MAP_SHARED, arg0, arg3);
+		if (map == MAP_FAILED) {
+			*ret = -1;
+			break;
+		}
+
+		adr = arg2;
+		ofs = 0;
+		for (i = 0; i < arg1; i++) {
+			memBBRd(adr, (BYTE *)&iov, sizeof(iov));
+			if (ofs + iov.len >= size)
+				iov.len = size - ofs;
+			memBBWrt_opt(iov.addr, map + ofs, iov.len);
+			ofs += iov.len;
+			adr += sizeof(iov);
+		}
+
+		munmap(map, size);
+
+		if (!addSscReq(arg0, size))
+			*ret = -1;
+		break;
+	}
+
+	case SSC_WAIT_COMPLETION: {
+		SscDiskStat stat;
+		SscReqNode *p;
+
+		memBBRd(arg0, (BYTE *)&stat, sizeof(stat));
+		*ret = -1;
+		for (p = sscPend; p; p = p->next) {
+			if (p->fd == stat.fd) {
+				stat.count = p->count;
+				delSscReq(p);
+				memBBWrt(arg0, (BYTE *)&stat, sizeof(stat));
+				*ret = 0;
+				break;
+			}
+		}
+		break;
+	}
+
+	case SSC_WRITE: {
+		SscDiskReq iov;
+		off_t filesize;
+		size_t size, ofs;
+		ADDR adr;
+		char *map;
+		int i;
+
+		filesize = lseek(arg0, 0LL, SEEK_END);
+		if (filesize == -1 || arg3 > filesize) {
+			*ret = -1;
+			break;
+		}
+
+		size = 0;
+		adr = arg2;
+		for (i = 0; i < arg1; i++) {
+			memBBRd(adr, (BYTE *)&iov, sizeof(iov));
+			size += iov.len;
+			adr += sizeof(iov);
+		}
+
+		if (arg3 + size > filesize)
+			size = filesize - arg3;
+
+		map = mmap(0, size, PROT_WRITE, MAP_SHARED, arg0, arg3);
+		if (map == MAP_FAILED) {
+			*ret = -1;
+			break;
+		}
+
+		adr = arg2;
+		ofs = 0;
+		for (i = 0; i < arg1; i++) {
+			memBBRd(adr, (BYTE *)&iov, sizeof(iov));
+			if (ofs + iov.len >= size)
+				iov.len = size - ofs;
+			memBBRd(iov.addr, map, iov.len);
+			ofs += iov.len;
+			adr += sizeof(iov);
+		}
+
+		munmap(map, size);
+
+		if (!addSscReq(arg0, size))
+			*ret = -1;
+		break;
+	}
+
+#ifdef NEW_MP
+	case SSC_SAL_SET_VECTORS:
+		if (arg0 == 2) {
+			os_boot_rendez_ip=arg1;
+			os_boot_rendez_gp=arg2;
+		}
+		break;
+#endif
+
+	default:
+		progExit("SSC #%d: unimplemented call\n", num);
+		break;
+	}
+
+	PSR_DT = olddt;
+}