Home | History | Annotate | Line # | Download | only in test
stand_user.c revision 1.4.26.1
      1  1.4.26.1     rmind /*	$NetBSD: stand_user.c,v 1.4.26.1 2007/03/12 05:48:39 rmind 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.1  drochner int samain __P((void));
     53       1.1  drochner 
     54       1.1  drochner int
     55       1.1  drochner main()
     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.1  drochner 	return (samain());
     61       1.1  drochner }
     62       1.1  drochner 
     63       1.1  drochner void
     64       1.1  drochner _rtt()
     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.1  drochner getsecs()
     72       1.1  drochner {
     73       1.1  drochner 	struct timeval t;
     74       1.1  drochner 	gettimeofday(&t, 0);
     75       1.1  drochner 	return (t.tv_sec);
     76       1.1  drochner }
     77       1.1  drochner 
     78       1.1  drochner void
     79       1.1  drochner delay(t)
     80       1.1  drochner 	int t;
     81       1.1  drochner {
     82       1.1  drochner 	struct timeval to;
     83       1.1  drochner 	to.tv_sec = 0;
     84       1.1  drochner 	to.tv_usec = t;
     85       1.1  drochner 	select(0, 0, 0, 0, &to);
     86       1.1  drochner }
     87       1.1  drochner 
     88       1.1  drochner /* make output appear unbuffered */
     89       1.1  drochner void
     90       1.1  drochner saputchar(c)
     91       1.1  drochner 	int c;
     92       1.1  drochner {
     93       1.1  drochner 	putchar(c);
     94       1.1  drochner 	fflush(stdout);
     95       1.1  drochner }
     96       1.1  drochner 
     97       1.1  drochner /*
     98       1.1  drochner  * some functions to get access to the hardware
     99       1.1  drochner  */
    100       1.1  drochner 
    101       1.1  drochner static int memfd, memcnt;
    102       1.1  drochner 
    103  1.4.26.1     rmind void *
    104       1.1  drochner mapmem(offset, len)
    105       1.1  drochner 	int offset, len;
    106       1.1  drochner {
    107  1.4.26.1     rmind 	void *base;
    108       1.1  drochner 
    109       1.1  drochner 	if (memcnt == 0)
    110       1.1  drochner 		memfd = open("/dev/mem", O_RDWR, 0);
    111       1.1  drochner 	if (memfd < 0) {
    112       1.1  drochner 		warn("open /dev/mem");
    113       1.1  drochner 		return (0);
    114       1.1  drochner 	}
    115       1.1  drochner 	base = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED,
    116       1.1  drochner 		    memfd, offset);
    117  1.4.26.1     rmind 	if (base == (void *)-1) {
    118       1.1  drochner 		warn("mmap %x-%x", offset, offset + len - 1);
    119       1.1  drochner 		return (0);
    120       1.1  drochner 	}
    121       1.1  drochner 	memcnt++;
    122       1.1  drochner 	return (base);
    123       1.1  drochner }
    124       1.1  drochner 
    125       1.1  drochner void
    126       1.1  drochner unmapmem(addr, len)
    127  1.4.26.1     rmind 	void *addr;
    128       1.1  drochner 	int len;
    129       1.1  drochner {
    130       1.1  drochner 	munmap(addr, len);
    131       1.1  drochner 	memcnt--;
    132       1.1  drochner 	if (memcnt == 0)
    133       1.1  drochner 		close(memfd);
    134       1.1  drochner }
    135       1.1  drochner 
    136       1.1  drochner int
    137       1.1  drochner mapio()
    138       1.1  drochner {
    139       1.1  drochner 	int res;
    140       1.1  drochner 
    141       1.1  drochner 	res = i386_iopl(1);
    142       1.1  drochner 	if (res)
    143       1.1  drochner 		warn("i386_iopl");
    144       1.1  drochner 	return (res);
    145       1.1  drochner }
    146       1.2  drochner 
    147       1.2  drochner int ourseg = 12345;
    148