summaryrefslogtreecommitdiffstats
path: root/qemu/roms/openbios/arch/unix/unix.c
blob: 1f628eb78fe7d4f058e34bc32fc1b8eceba3936f (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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/* tag: hosted forth environment, executable code
 *
 * Copyright (C) 2003-2005 Patrick Mauritz, Stefan Reinauer
 *
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#define __USE_LARGEFILE64
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>

#ifdef __GLIBC__
#define _GNU_SOURCE
#include <getopt.h>
#endif

#include "sysinclude.h"
#include "mconfig.h"
#include "config.h"
#include "kernel/kernel.h"
#include "dict.h"
#include "kernel/stack.h"
#include "arch/unix/plugins.h"
#include "libopenbios/bindings.h"
#include "libopenbios/console.h"
#include "libopenbios/openbios.h"
#include "openbios-version.h"

#include "blk.h"
#include "libopenbios/ofmem.h"

#define MEMORY_SIZE	(4*1024*1024)	/* 4M ram for hosted system */
#define DICTIONARY_SIZE	(256*1024)	/* 256k for the dictionary   */

#if defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS==64)
#define lseek lseek64
#define __LFS O_LARGEFILE
#else
#define __LFS 0
#endif

/* prototypes */
static void exit_terminal(void);
void boot(void);

unsigned long virt_offset = 0;

/* local variables */

static ucell *memory;

static int diskemu;

static int segfault = 0;
static int verbose = 0;

#if defined(CONFIG_PPC) || defined(CONFIG_SPARC64)
unsigned long isa_io_base;
#endif

int errno_int;	/* implement for fs drivers, needed to build on Mac OS X */

ucell ofmem_claim(ucell addr, ucell size, ucell align)
{
    return 0;
}

#ifdef CONFIG_PPC
extern void flush_icache_range(char *start, char *stop);

void flush_icache_range(char *start, char *stop)
{
}
#endif

#ifdef CONFIG_PPC
/* Expose system level is_machine helpers to make generic code easier */

#include "drivers/drivers.h"
int is_apple(void)
{
    return 0;
}

int is_oldworld(void)
{
    return 0;
}

int is_newworld(void)
{
    return 0;
}
#endif

#if 0
static void write_dictionary(char *filename)
{
	FILE *f;
	xt_t initxt;

	initxt = findword("initialize-of");
	if (!initxt)
		printk("warning: dictionary needs word called initialize-of\n");

	f = fopen(filename, "w");
	if (!f) {
		printk("panic: can't open dictionary.\n");
		exit_terminal();
		exit(1);
	}

	fwrite(DICTID, 16, 1, f);
	fwrite(dict, dicthead, 1, f);

	/* Write start address and last to relocate on load */
	fwrite(&dict, sizeof(ucell), 1, f);
	fwrite(&last, sizeof(ucell), 1, f);

	fclose(f);

#ifdef CONFIG_DEBUG_DICTIONARY
	printk("wrote dictionary to file %s.\n", filename);
#endif
}
#endif

static ucell read_dictionary(char *fil)
{
	int ilen;
	ucell ret;
	char *mem;
	FILE *f;
	struct stat finfo;

	if (stat(fil, &finfo))
		return 0;

	ilen = finfo.st_size;

	if ((mem = malloc(ilen)) == NULL) {
		printk("panic: not enough memory.\n");
		exit_terminal();
		exit(1);
	}

	f = fopen(fil, "r");
	if (!f) {
		printk("panic: can't open dictionary.\n");
		exit_terminal();
		exit(1);
	}

	if (fread(mem, ilen, 1, f) != 1) {
		printk("panic: can't read dictionary.\n");
		fclose(f);
		exit_terminal();
		exit(1);
	}
	fclose(f);

	ret = load_dictionary(mem, ilen);

	free(mem);
	return ret;
}


/*
 * functions used by primitives
 */

static int unix_availchar(void)
{
	int tmp = getc(stdin);
	if (tmp != EOF) {
		ungetc(tmp, stdin);
		return -1;
	}
	return 0;
}

static int unix_putchar(int c)
{
	putc(c, stdout);
	return c;
}

static int unix_getchar(void)
{
	return getc(stdin);
}

static struct _console_ops unix_console_ops = {
	.putchar = unix_putchar,
	.availchar = unix_availchar,
	.getchar = unix_getchar
};

u8 inb(u32 reg)
{
#ifdef CONFIG_PLUGINS
	io_ops_t *ior = find_iorange(reg);
	if (ior)
		return ior->inb(reg);
#endif

	printk("TRAP: io byte read @0x%x", reg);
	return 0xff;
}

u16 inw(u32 reg)
{
#ifdef CONFIG_PLUGINS
	io_ops_t *ior = find_iorange(reg);
	if (ior)
		return ior->inw(reg);
#endif

	printk("TRAP: io word read @0x%x", reg);
	return 0xffff;
}

u32 inl(u32 reg)
{
#ifdef CONFIG_PLUGINS
	io_ops_t *ior = find_iorange(reg);
	if (ior)
		return ior->inl(reg);
#endif

	printk("TRAP: io long read @0x%x", reg);
	return 0xffffffff;
}

void outb(u32 reg, u8 val)
{
#ifdef CONFIG_PLUGINS
	io_ops_t *ior = find_iorange(reg);
	if (ior) {
		ior->outb(reg, val);
		return;
	}
#endif

	printk("TRAP: io byte write 0x%x -> 0x%x", val, reg);
}

void outw(u32 reg, u16 val)
{
#ifdef CONFIG_PLUGINS
	io_ops_t *ior = find_iorange(reg);
	if (ior) {
		ior->outw(reg, val);
		return;
	}
#endif
	printk("TRAP: io word write 0x%x -> 0x%x", val, reg);
}

void outl(u32 reg, u32 val)
{
#ifdef CONFIG_PLUGINS
	io_ops_t *ior = find_iorange(reg);
	if (ior) {
		ior->outl(reg, val);
		return;
	}
#endif
	printk("TRAP: io long write 0x%x -> 0x%x", val, reg);
}

/*
 * terminal initialization and cleanup.
 */

static struct termios saved_termios;

static void init_terminal(void)
{
	struct termios termios;

	tcgetattr(0, &saved_termios);
	tcgetattr(0, &termios);
	termios.c_lflag &= ~(ICANON | ECHO);
        termios.c_cc[VMIN] = 1;
        termios.c_cc[VTIME] = 3; // 300 ms
	tcsetattr(0, 0, &termios);
}

static void exit_terminal(void)
{
	tcsetattr(0, 0, &saved_termios);
}

/*
 *  segmentation fault handler. linux specific?
 */

static void
segv_handler(int signo __attribute__ ((unused)),
	     siginfo_t * si, void *context __attribute__ ((unused)))
{
	static int count = 0;
	ucell addr = 0xdeadbeef;

	if (count) {
		printk("Died while dumping forth dictionary core.\n");
		goto out;
	}

	count++;

	if (PC >= (ucell) dict && PC <= (ucell) dict + dicthead)
		addr = *(ucell *) PC;

	printk("panic: segmentation violation at %x\n", (ucell)si->si_addr);
	printk("dict=0x%x here=0x%x(dict+0x%x) pc=0x%x(dict+0x%x)\n",
	       (ucell)dict, (ucell)dict + dicthead, dicthead, PC, PC - (ucell) dict);
	printk("dstackcnt=%d rstackcnt=%d instruction=%x\n",
	       dstackcnt, rstackcnt, addr);

#ifdef CONFIG_DEBUG_DSTACK
	printdstack();
#endif
#ifdef CONFIG_DEBUG_RSTACK
	printrstack();
#endif
#if 0
	printk("Writing dictionary core file\n");
	write_dictionary("forth.dict.core");
#endif

      out:
	exit_terminal();
	exit(1);
}

/*
 *  Interrupt handler. linux specific?
 *  Restore terminal state on ctrl-C.
 */

static void
int_handler(int signo __attribute__ ((unused)),
            siginfo_t * si __attribute__ ((unused)),
            void *context __attribute__ ((unused)))
{
    printk("\n");
    exit_terminal();
    exit(1);
}

/*
 * allocate memory and prepare engine for memory management.
 */

static void init_memory(void)
{
	memory = malloc(MEMORY_SIZE);
	if (!memory) {
		printk("panic: not enough memory on host system.\n");
		exit_terminal();
		exit(1);
	}

	memset (memory, 0, MEMORY_SIZE);
	/* we push start and end of memory to the stack
	 * so that it can be used by the forth word QUIT
	 * to initialize the memory allocator
	 */

	PUSH((ucell) memory);
	PUSH((ucell) memory + MEMORY_SIZE);
}

void exception(__attribute__((unused)) cell no)
{
	/*
	 * this is a noop since the dictionary has to take care
	 * itself of errors it generates outside of the bootstrap
	 */
}

static void
arch_init( void )
{
	openbios_init();
	modules_init();
	if(diskemu!=-1)
		blk_init();

	device_end();
        bind_func("platform-boot", boot);
}

int
read_from_disk( int channel, int unit, int blk, unsigned long mphys, int size )
{
	// channels and units not supported yet.
	unsigned char *buf=(unsigned char *)mphys;

	if(diskemu==-1)
		return -1;

	//printk("read: ch=%d, unit=%d, blk=%ld, phys=%lx, size=%d\n",
	//		channel, unit, blk, mphys, size);

	lseek(diskemu, (ducell)blk*512, SEEK_SET);
	read(diskemu, buf, size);

	return 0;
}

/*
 * main loop
 */

#define BANNER	"OpenBIOS core. (C) 2003-2006 Patrick Mauritz, Stefan Reinauer\n"\
		"This software comes with absolutely no warranty. "\
		"All rights reserved.\n\n"


#define USAGE   "usage: %s [options] [dictionary file|source file]\n\n"

int main(int argc, char *argv[])
{
	struct sigaction sa;
#if 0
	unsigned char *dictname = NULL;
#endif
	int c;

	const char *optstring = "VvhsD:P:p:f:?";

	while (1) {
#ifdef __GLIBC__
		int option_index = 0;
		static struct option long_options[] = {
			{"version", 0, NULL, 'V'},
			{"verbose", 0, NULL, 'v'},
			{"help", 0, NULL, 'h'},
//			{"dictionary", 1, NULL, 'D'},
			{"segfault", 0, NULL, 's'},
#ifdef CONFIG_PLUGINS
			{"plugin-path", 1, NULL, 'P'},
			{"plugin", 1, NULL, 'p'},
#endif
			{"file", 1, NULL, 'f'}
		};

		c = getopt_long(argc, argv, optstring, long_options,
				&option_index);
#else
		c = getopt(argc, argv, optstring);
#endif
		if (c == -1)
			break;

		switch (c) {
		case 'V':
                        printk(BANNER "Version " OPENBIOS_VERSION_STR "\n");
			return 0;
		case 'h':
		case '?':
                        printk(BANNER "Version " OPENBIOS_VERSION_STR "\n"
                               USAGE, argv[0]);
			return 0;
		case 'v':
			verbose = 1;
			break;
		case 's':
			segfault = 1;
			break;
#if 0
		case 'D':
			printk("Dumping final dictionary to '%s'\n", optarg);
			dictname = optarg;
			break;
#endif
#ifdef CONFIG_PLUGINS
		case 'P':
			printk("Plugin search path is now '%s'\n", optarg);
			plugindir = optarg;
			break;
		case 'p':
			printk("Loading plugin %s\n", optarg);
			load_plugin(optarg);
			break;
#endif
		case 'f':
			diskemu=open(optarg, O_RDONLY|__LFS);
			if(diskemu!=-1)
				printk("Using %s as harddisk.\n", optarg);
			else
				printk("%s not found. no harddisk node.\n",
						optarg);
			break;
		default:
			return 1;
		}
	}

	if (argc < optind + 1) {
		printk(USAGE, argv[0]);
		return 1;
	}

	/* Initialise console */
	init_console(unix_console_ops);

	if ((dict = (unsigned char *) malloc(DICTIONARY_SIZE)) == NULL) {
		printk("panic: not enough memory.\n");
		return 1;
	}

	dictlimit = DICTIONARY_SIZE;
	memset(dict, 0, DICTIONARY_SIZE);

	if (!segfault) {
		if (verbose)
			printk("Installing SIGSEGV handler...");

		sa.sa_sigaction = segv_handler;
		sigemptyset(&sa.sa_mask);
		sa.sa_flags = SA_SIGINFO | SA_NODEFER;
		sigaction(SIGSEGV, &sa, NULL);

		if (verbose)
			printk("done.\n");
	}

	/* set terminal to do non blocking reads */
	init_terminal();

        if (verbose)
            printk("Installing SIGINT handler...");

        sa.sa_sigaction = int_handler;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = SA_SIGINFO | SA_NODEFER;
        sigaction(SIGINT, &sa, NULL);

        if (verbose)
            printk("done.\n");

	read_dictionary(argv[optind]);
	forth_init();

	PUSH_xt( bind_noname_func(arch_init) );
	fword("PREPOST-initializer");

	PC = (cell)findword("initialize-of");
	if (PC) {
		if (verbose) {
			if (optind + 1 != argc)
				printk("Warning: only first dictionary used.\n");

			printk("dictionary loaded (%d bytes).\n", dicthead);
			printk("Initializing memory...");
		}
		init_memory();

		if (verbose) {
			printk("done\n");

			printk("Jumping to dictionary...");
		}

		enterforth((xt_t)PC);
#if 0
		if (dictname != NULL)
			write_dictionary(dictname);
#endif

		free(memory);

	} else {		/* input file is not a dictionary */
		printk("not supported.\n");
	}

	exit_terminal();
	if (diskemu!=-1)
		close(diskemu);

	free(dict);
	return 0;
}

#undef printk
int
printk( const char *fmt, ... )
{
	int i;

	va_list args;
	va_start( args, fmt );
	i = vprintf(fmt, args );
	va_end( args );
	return i;
}