xref: /netbsd/sys/arch/i386/stand/lib/test/stand_user.c (revision bf9ec67e)
1 /*	$NetBSD: stand_user.c,v 1.2 1998/11/22 15:44:03 drochner Exp $	*/
2 
3 /*
4  * Copyright (c) 1998
5  *	Matthias Drochner.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed for the NetBSD Project
18  *	by Matthias Drochner.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #include <lib/libsa/stand.h>
36 
37 #include "sanamespace.h"
38 
39 #include <stdio.h>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <stdarg.h>
43 #include <sys/time.h>
44 #include <fcntl.h>
45 #include <sys/mman.h>
46 #include <machine/sysarch.h>
47 #include <err.h>
48 
49 /*
50  * Harness for test of standalone code in user space.
51  * XXX Requires silly namespace games.
52  */
53 
54 #ifndef HEAPSIZE
55 #define HEAPSIZE (128*1024)
56 #endif
57 
58 int samain __P((void));
59 
60 int
61 main()
62 {
63 	char *h = malloc(HEAPSIZE);
64 	setheap(h, h + HEAPSIZE);
65 
66 	return (samain());
67 }
68 
69 void
70 _rtt()
71 {
72 	warnx("_rtt called");
73 	_exit(1);
74 }
75 
76 int
77 getsecs()
78 {
79 	struct timeval t;
80 	gettimeofday(&t, 0);
81 	return (t.tv_sec);
82 }
83 
84 void
85 delay(t)
86 	int t;
87 {
88 	struct timeval to;
89 	to.tv_sec = 0;
90 	to.tv_usec = t;
91 	select(0, 0, 0, 0, &to);
92 }
93 
94 /* make output appear unbuffered */
95 void
96 saputchar(c)
97 	int c;
98 {
99 	putchar(c);
100 	fflush(stdout);
101 }
102 
103 /*
104  * some functions to get access to the hardware
105  */
106 
107 static int memfd, memcnt;
108 
109 caddr_t
110 mapmem(offset, len)
111 	int offset, len;
112 {
113 	caddr_t base;
114 
115 	if (memcnt == 0)
116 		memfd = open("/dev/mem", O_RDWR, 0);
117 	if (memfd < 0) {
118 		warn("open /dev/mem");
119 		return (0);
120 	}
121 	base = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED,
122 		    memfd, offset);
123 	if (base == (caddr_t)-1) {
124 		warn("mmap %x-%x", offset, offset + len - 1);
125 		return (0);
126 	}
127 	memcnt++;
128 	return (base);
129 }
130 
131 void
132 unmapmem(addr, len)
133 	caddr_t addr;
134 	int len;
135 {
136 	munmap(addr, len);
137 	memcnt--;
138 	if (memcnt == 0)
139 		close(memfd);
140 }
141 
142 int
143 mapio()
144 {
145 	int res;
146 
147 	res = i386_iopl(1);
148 	if (res)
149 		warn("i386_iopl");
150 	return (res);
151 }
152 
153 int ourseg = 12345;
154