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