machdep.c revision 1.6.10.1       1  1.6.10.1  pgoyette /*	$NetBSD: machdep.c,v 1.6.10.1 2017/01/07 08:56:16 pgoyette Exp $	*/
      2       1.1    bouyer 
      3       1.1    bouyer /*
      4       1.1    bouyer  * Copyright 2001, 2002 Wasabi Systems, Inc.
      5       1.1    bouyer  * All rights reserved.
      6       1.1    bouyer  *
      7       1.1    bouyer  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
      8       1.1    bouyer  *
      9       1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     10       1.1    bouyer  * modification, are permitted provided that the following conditions
     11       1.1    bouyer  * are met:
     12       1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     13       1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     14       1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     16       1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     17       1.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     18       1.1    bouyer  *    must display the following acknowledgement:
     19       1.1    bouyer  *      This product includes software developed for the NetBSD Project by
     20       1.1    bouyer  *      Wasabi Systems, Inc.
     21       1.1    bouyer  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22       1.1    bouyer  *    or promote products derived from this software without specific prior
     23       1.1    bouyer  *    written permission.
     24       1.1    bouyer  *
     25       1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26       1.1    bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27       1.1    bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28       1.1    bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29       1.1    bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30       1.1    bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31       1.1    bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32       1.1    bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33       1.1    bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34       1.1    bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35       1.1    bouyer  * POSSIBILITY OF SUCH DAMAGE.
     36       1.1    bouyer  */
     37       1.1    bouyer 
     38       1.1    bouyer /*
     39       1.1    bouyer  * Copyright (c) 1988 University of Utah.
     40       1.1    bouyer  * Copyright (c) 1992, 1993
     41       1.1    bouyer  *	The Regents of the University of California.  All rights reserved.
     42       1.1    bouyer  *
     43       1.1    bouyer  * This code is derived from software contributed to Berkeley by
     44       1.1    bouyer  * the Systems Programming Group of the University of Utah Computer
     45       1.1    bouyer  * Science Department, The Mach Operating System project at
     46       1.1    bouyer  * Carnegie-Mellon University and Ralph Campbell.
     47       1.1    bouyer  *
     48       1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     49       1.1    bouyer  * modification, are permitted provided that the following conditions
     50       1.1    bouyer  * are met:
     51       1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     52       1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     53       1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     54       1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     55       1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     56       1.1    bouyer  * 3. Neither the name of the University nor the names of its contributors
     57       1.1    bouyer  *    may be used to endorse or promote products derived from this software
     58       1.1    bouyer  *    without specific prior written permission.
     59       1.1    bouyer  *
     60       1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61       1.1    bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62       1.1    bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63       1.1    bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64       1.1    bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65       1.1    bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66       1.1    bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67       1.1    bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68       1.1    bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69       1.1    bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70       1.1    bouyer  * SUCH DAMAGE.
     71       1.1    bouyer  *
     72       1.1    bouyer  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
     73       1.1    bouyer  *	from: Utah Hdr: machdep.c 1.63 91/04/24
     74       1.1    bouyer  */
     75       1.1    bouyer 
     76       1.1    bouyer /*
     77       1.1    bouyer  * Copyright (c) 2009, 2010 Miodrag Vallat.
     78       1.1    bouyer  *
     79       1.1    bouyer  * Permission to use, copy, modify, and distribute this software for any
     80       1.1    bouyer  * purpose with or without fee is hereby granted, provided that the above
     81       1.1    bouyer  * copyright notice and this permission notice appear in all copies.
     82       1.1    bouyer  *
     83       1.1    bouyer  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     84       1.1    bouyer  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     85       1.1    bouyer  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     86       1.1    bouyer  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     87       1.1    bouyer  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     88       1.1    bouyer  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     89       1.1    bouyer  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     90       1.1    bouyer  */
     91       1.1    bouyer 
     92       1.1    bouyer #include <sys/cdefs.h>
     93  1.6.10.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.6.10.1 2017/01/07 08:56:16 pgoyette Exp $");
     94       1.1    bouyer 
     95       1.1    bouyer #include "opt_ddb.h"
     96       1.1    bouyer #include "opt_execfmt.h"
     97       1.1    bouyer #include "opt_modular.h"
     98       1.1    bouyer 
     99       1.1    bouyer #define _MIPS_BUS_DMA_PRIVATE
    100       1.1    bouyer 
    101       1.1    bouyer #include <sys/param.h>
    102       1.1    bouyer #include <sys/systm.h>
    103       1.1    bouyer #include <sys/kernel.h>
    104       1.1    bouyer #include <sys/buf.h>
    105       1.1    bouyer #include <sys/reboot.h>
    106       1.1    bouyer #include <sys/mount.h>
    107       1.1    bouyer #include <sys/kcore.h>
    108       1.1    bouyer #include <sys/boot_flag.h>
    109       1.1    bouyer #include <sys/termios.h>
    110       1.1    bouyer #include <sys/ksyms.h>
    111       1.1    bouyer #include <sys/device.h>
    112       1.6  christos #include <sys/cpu.h>
    113       1.1    bouyer 
    114       1.1    bouyer #include <uvm/uvm_extern.h>
    115       1.1    bouyer 
    116       1.1    bouyer #include <dev/cons.h>
    117       1.1    bouyer 
    118       1.1    bouyer #include "ksyms.h"
    119       1.1    bouyer 
    120       1.1    bouyer #if NKSYMS || defined(DDB) || defined(MODULAR)
    121       1.1    bouyer #include <machine/db_machdep.h>
    122       1.1    bouyer #include <ddb/db_extern.h>
    123       1.1    bouyer #include <sys/exec_elf.h>
    124       1.1    bouyer #endif
    125       1.1    bouyer 
    126       1.1    bouyer #include <evbmips/loongson/autoconf.h>
    127       1.1    bouyer #include <evbmips/loongson/loongson_intr.h>
    128       1.1    bouyer #include <evbmips/loongson/loongson_bus_defs.h>
    129       1.1    bouyer #include <machine/cpu.h>
    130       1.1    bouyer #include <machine/psl.h>
    131       1.1    bouyer 
    132       1.1    bouyer #include <mips/locore.h>
    133       1.1    bouyer 
    134       1.1    bouyer #include <mips/bonito/bonitoreg.h>
    135       1.1    bouyer #include <mips/bonito/bonitovar.h>
    136       1.1    bouyer #include <mips/pmon/pmon.h>
    137       1.4    nonaka 
    138       1.4    nonaka #include "sisfb.h"
    139       1.4    nonaka #if NSISFB > 0
    140       1.1    bouyer #include <dev/pci/sisfb.h>
    141       1.4    nonaka #endif
    142       1.4    nonaka 
    143       1.4    nonaka #include "lynxfb.h"
    144       1.4    nonaka #if NLYNXFB > 0
    145       1.4    nonaka #include <dev/pci/lynxfbvar.h>
    146       1.4    nonaka #endif
    147       1.4    nonaka 
    148       1.4    nonaka #include "pckbc.h"
    149       1.4    nonaka #if NPCKBC > 0
    150       1.4    nonaka #include <dev/isa/isareg.h>
    151       1.4    nonaka #include <dev/ic/i8042reg.h>
    152       1.4    nonaka #include <dev/ic/pckbcvar.h>
    153       1.4    nonaka #endif
    154       1.4    nonaka #include "pckbd.h"
    155       1.4    nonaka #include "ukbd.h"
    156       1.4    nonaka #if NUKBD > 0
    157       1.4    nonaka #include <dev/usb/ukbdvar.h>
    158       1.4    nonaka #endif
    159       1.4    nonaka #if NPCKBD > 0 || NUKBD > 0
    160       1.4    nonaka #include <dev/wscons/wskbdvar.h>
    161       1.4    nonaka #endif
    162       1.1    bouyer 
    163       1.1    bouyer #include "com.h"
    164       1.1    bouyer #if NCOM > 0
    165       1.1    bouyer #include <dev/ic/comreg.h>
    166       1.1    bouyer #include <dev/ic/comvar.h>
    167       1.1    bouyer 
    168       1.2  macallan bus_space_tag_t comconsiot;
    169       1.2  macallan bus_addr_t comconsaddr;
    170       1.2  macallan int comconsrate = 0;
    171       1.2  macallan #endif /* NCOM > 0 */
    172       1.2  macallan 
    173       1.1    bouyer #ifdef LOW_DEBUG
    174       1.1    bouyer #define DPRINTF(x) printf x
    175       1.1    bouyer #define DPPRINTF(x) pmon_printf x
    176       1.1    bouyer #else
    177       1.1    bouyer #define DPRINTF(x)
    178       1.1    bouyer #define DPPRINTF(x)
    179       1.1    bouyer #endif
    180       1.1    bouyer 
    181       1.1    bouyer 
    182       1.1    bouyer int ex_mallocsafe = 0;
    183       1.1    bouyer struct extent *loongson_io_ex = NULL;
    184       1.1    bouyer struct extent *loongson_mem_ex = NULL;
    185       1.1    bouyer struct mips_bus_space bonito_iot;
    186       1.1    bouyer struct mips_bus_space bonito_memt;
    187       1.1    bouyer struct mips_bus_dma_tag bonito_dmat;
    188       1.1    bouyer struct mips_pci_chipset bonito_pc;
    189       1.1    bouyer 
    190       1.1    bouyer uint loongson_ver;
    191       1.1    bouyer 
    192       1.1    bouyer const struct platform *sys_platform;
    193       1.1    bouyer struct bonito_flavour {
    194       1.1    bouyer 	const char *prefix;
    195       1.1    bouyer 	const struct platform *platform;
    196       1.1    bouyer };
    197       1.1    bouyer 
    198       1.1    bouyer extern const struct platform fuloong_platform;
    199       1.1    bouyer extern const struct platform gdium_platform;
    200       1.1    bouyer extern const struct platform generic2e_platform;
    201       1.1    bouyer extern const struct platform lynloong_platform;
    202       1.1    bouyer extern const struct platform yeeloong_platform;
    203       1.1    bouyer 
    204       1.1    bouyer const struct bonito_flavour bonito_flavours[] = {
    205       1.1    bouyer 	/* Lemote Fuloong 2F mini-PC */
    206       1.1    bouyer 	{ "LM6002",	&fuloong_platform }, /* dual Ethernet, no prefix */
    207       1.1    bouyer 	{ "LM6003",	&fuloong_platform },
    208       1.1    bouyer 	{ "LM6004",	&fuloong_platform },
    209       1.1    bouyer 	/* EMTEC Gdium Liberty 1000 */
    210       1.1    bouyer 	{ "Gdium",	&gdium_platform },
    211       1.1    bouyer 	/* Lemote Yeeloong 8.9" netbook */
    212       1.1    bouyer 	{ "LM8089",	&yeeloong_platform },
    213       1.1    bouyer 	/* supposedly Lemote Yeeloong 10.1" netbook, but those found so far
    214       1.1    bouyer 	   report themselves as LM8089 */
    215       1.1    bouyer 	{ "LM8101",	&yeeloong_platform },
    216       1.1    bouyer 	/* Lemote Lynloong all-in-one computer */
    217       1.1    bouyer 	{ "LM9001",	&lynloong_platform },
    218       1.1    bouyer 	{ NULL }
    219       1.1    bouyer };
    220       1.1    bouyer 
    221       1.1    bouyer /* Maps for VM objects. */
    222       1.1    bouyer struct vm_map *phys_map = NULL;
    223       1.1    bouyer 
    224       1.1    bouyer int	netboot;		/* Are we netbooting? */
    225       1.1    bouyer 
    226       1.1    bouyer phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    227       1.1    bouyer int mem_cluster_cnt;
    228       1.1    bouyer 
    229       1.1    bouyer void	mach_init(int, int32_t, int32_t, int32_t, char *);
    230       1.1    bouyer 
    231       1.1    bouyer static int  pmoncngetc(dev_t);
    232       1.1    bouyer static void pmoncnputc(dev_t, int);
    233       1.1    bouyer 
    234       1.1    bouyer struct consdev pmoncons = {
    235       1.1    bouyer         NULL,		/* probe */
    236       1.1    bouyer 	NULL, 		/* init */
    237       1.1    bouyer 	pmoncngetc, 	/* getc */
    238       1.1    bouyer 	pmoncnputc,	/* putc */
    239       1.1    bouyer 	nullcnpollc,	/* poolc */
    240       1.1    bouyer 	NULL,		/* BELL */
    241       1.1    bouyer 	makedev(0, 0),
    242       1.1    bouyer 	CN_DEAD
    243       1.1    bouyer };
    244       1.1    bouyer 
    245       1.1    bouyer /*
    246       1.1    bouyer  * Do all the stuff that locore normally does before calling main().
    247       1.1    bouyer  */
    248       1.1    bouyer void
    249       1.1    bouyer mach_init(int32_t argc, int32_t argva, int32_t enva, int32_t callvec,
    250       1.1    bouyer     char *boot_esym)
    251       1.1    bouyer {
    252       1.1    bouyer 	void *kernend;
    253       1.1    bouyer #ifdef NOTYET
    254       1.1    bouyer 	int howto;
    255       1.1    bouyer #endif
    256       1.1    bouyer 	const char *env;
    257       1.1    bouyer 	int i;
    258       1.1    bouyer 	psize_t memlo, memhi;
    259       1.1    bouyer 	const struct bonito_flavour *f;
    260       1.1    bouyer 	char *ssym = NULL, *esym = NULL;
    261       1.1    bouyer 	pcireg_t reg;
    262       1.1    bouyer 	pcitag_t pcitag;
    263       1.1    bouyer 
    264       1.1    bouyer 	extern char edata[], end[];
    265       1.1    bouyer 
    266       1.1    bouyer 	/*
    267       1.1    bouyer 	 * Clear the BSS segment.
    268       1.1    bouyer 	 */
    269       1.1    bouyer 	memset(edata, 0, (char *)end - edata);
    270       1.1    bouyer 
    271       1.1    bouyer 	pmon_init(argc, argva, enva, callvec);
    272       1.1    bouyer 	DPPRINTF(("pmon hello\n"));
    273       1.1    bouyer 
    274       1.1    bouyer 	cn_tab = &pmoncons;
    275       1.1    bouyer 
    276       1.1    bouyer 	DPRINTF(("hello 0x%x %d 0x%x 0x%x %p stack %p\n", pmon_callvec, argc, argva, enva, boot_esym, &i));
    277       1.1    bouyer 
    278       1.1    bouyer 	/*
    279       1.1    bouyer 	 * Reserve space for the symbol table, if it exists.
    280       1.1    bouyer 	 */
    281       1.1    bouyer 
    282       1.1    bouyer #if NKSYMS || defined(DDB) || defined(MODULAR)
    283       1.1    bouyer 	/* Attempt to locate ELF header and symbol table after kernel. */
    284       1.1    bouyer 	if (end[0] == ELFMAG0 && end[1] == ELFMAG1 &&
    285       1.1    bouyer 	    end[2] == ELFMAG2 && end[3] == ELFMAG3) {
    286       1.1    bouyer 		/* ELF header exists directly after kernel. */
    287       1.1    bouyer 		ssym = end;
    288       1.1    bouyer 		esym = boot_esym;
    289       1.1    bouyer 		kernend = (void *)mips_round_page(esym);
    290       1.1    bouyer 	} else {
    291       1.1    bouyer 		ssym = (char *)(vaddr_t)*(int32_t *)end;
    292       1.1    bouyer 		if (((long)ssym - (long)end) >= 0 &&
    293       1.1    bouyer 		    ((long)ssym - (long)end) <= 0x1000 &&
    294       1.1    bouyer 		    ssym[0] == ELFMAG0 && ssym[1] == ELFMAG1 &&
    295       1.1    bouyer 		    ssym[2] == ELFMAG2 && ssym[3] == ELFMAG3) {
    296       1.1    bouyer 			/* Pointers exist directly after kernel. */
    297       1.1    bouyer 			esym = (char *)(vaddr_t)*((int32_t *)end + 1);
    298       1.1    bouyer 			kernend = (void *)mips_round_page(esym);
    299       1.1    bouyer 		} else {
    300       1.1    bouyer 			/* Pointers aren't setup either... */
    301       1.1    bouyer 			ssym = NULL;
    302       1.1    bouyer 			esym = NULL;
    303       1.1    bouyer 			kernend = (void *)mips_round_page(end);
    304       1.1    bouyer 		}
    305       1.1    bouyer 	}
    306       1.1    bouyer 	DPRINTF(("ssym %p esym %p\n", ssym, esym));
    307       1.1    bouyer #endif
    308       1.1    bouyer 
    309       1.1    bouyer 	/*
    310       1.1    bouyer 	 * Set up the exception vectors and CPU-specific function
    311       1.1    bouyer 	 * vectors early on.  We need the wbflush() vector set up
    312       1.1    bouyer 	 * before comcnattach() is called (or at least before the
    313       1.1    bouyer 	 * first printf() after that is called).
    314       1.1    bouyer 	 * Also clears the I+D caches.
    315       1.1    bouyer 	 */
    316       1.1    bouyer 	DPRINTF(("mips_vector_init "));
    317       1.1    bouyer 	mips_vector_init(NULL, false);
    318       1.1    bouyer 
    319  1.6.10.1  pgoyette 	DPRINTF(("uvm_md_init\n"));
    320  1.6.10.1  pgoyette 	uvm_md_init();
    321       1.1    bouyer #if NKSYMS || defined(DDB) || defined(MODULAR)
    322       1.1    bouyer 	//ksyms_addsyms_elf((vaddr_t)esym - (vaddr_t)ssym, ssym, esym);
    323       1.1    bouyer #endif
    324       1.1    bouyer 
    325       1.1    bouyer 	/*
    326       1.1    bouyer 	 * Try and figure out what kind of hardware we are.
    327       1.1    bouyer 	 */
    328       1.1    bouyer 
    329       1.1    bouyer 	env = pmon_getenv("systype");
    330       1.1    bouyer 	if (env == NULL) {
    331       1.1    bouyer 		printf("Unable to figure out system type!\n");
    332       1.1    bouyer 		goto unsupported;
    333       1.1    bouyer 	}
    334       1.1    bouyer 	if (strcmp(env, "Bonito") != 0) {
    335       1.1    bouyer 		printf("This kernel doesn't support system type \"%s\".\n",
    336       1.1    bouyer 		    env);
    337       1.1    bouyer 		goto unsupported;
    338       1.1    bouyer 	}
    339       1.1    bouyer 
    340       1.1    bouyer 	/*
    341       1.1    bouyer 	 * While the kernel supports other processor types than Loongson,
    342       1.1    bouyer 	 * we are not expecting a Bonito-based system with a different
    343       1.1    bouyer 	 * processor.  Just to be on the safe side, refuse to run on
    344       1.1    bouyer 	 * non Loongson2 processors for now.
    345       1.1    bouyer 	 */
    346       1.1    bouyer 
    347       1.1    bouyer 	switch ((mips_options.mips_cpu_id >> 8) & 0xff) {
    348       1.1    bouyer 	case MIPS_LOONGSON2:
    349       1.1    bouyer 		switch (mips_options.mips_cpu_id & 0xff) {
    350       1.1    bouyer 		case 0x00:
    351       1.1    bouyer 			loongson_ver = 0x2c;
    352       1.1    bouyer 			break;
    353       1.1    bouyer 		case 0x02:
    354       1.1    bouyer 			loongson_ver = 0x2e;
    355       1.1    bouyer 			break;
    356       1.1    bouyer 		case 0x03:
    357       1.1    bouyer 			loongson_ver = 0x2f;
    358       1.1    bouyer 			break;
    359       1.1    bouyer 		case 0x05:
    360       1.1    bouyer 			loongson_ver = 0x3a;
    361       1.1    bouyer 			break;
    362       1.1    bouyer 		}
    363       1.1    bouyer 		if (loongson_ver == 0x2e || loongson_ver == 0x2f)
    364       1.1    bouyer 			break;
    365       1.1    bouyer 		/* FALLTHROUGH */
    366       1.1    bouyer 	default:
    367       1.1    bouyer 		printf("This kernel doesn't support processor type 0x%x"
    368       1.1    bouyer 		    ", version %d.%d.\n",
    369       1.1    bouyer 		    (mips_options.mips_cpu_id >> 8) & 0xff,
    370       1.1    bouyer 		    (mips_options.mips_cpu_id >> 4) & 0x0f,
    371       1.1    bouyer 		    mips_options.mips_cpu_id & 0x0f);
    372       1.1    bouyer 		goto unsupported;
    373       1.1    bouyer 	}
    374       1.1    bouyer 
    375       1.1    bouyer 	/*
    376       1.1    bouyer 	 * Try to figure out what particular machine we run on, depending
    377       1.1    bouyer 	 * on the PMON version information.
    378       1.1    bouyer 	 */
    379       1.1    bouyer 
    380       1.1    bouyer 	env = pmon_getenv("Version");
    381       1.1    bouyer 	if (env == NULL) {
    382       1.1    bouyer 		/*
    383       1.1    bouyer 		 * If this is a 2E system, use the generic code and hope
    384       1.1    bouyer 		 * for the best.
    385       1.1    bouyer 		 */
    386       1.1    bouyer 		if (loongson_ver == 0x2e) {
    387       1.1    bouyer 			sys_platform = &generic2e_platform;
    388       1.1    bouyer 		} else {
    389       1.1    bouyer 			printf("Unable to figure out model!\n");
    390       1.1    bouyer 			goto unsupported;
    391       1.1    bouyer 		}
    392       1.1    bouyer 	} else {
    393       1.1    bouyer 		for (f = bonito_flavours; f->prefix != NULL; f++)
    394       1.1    bouyer 			if (strncmp(env, f->prefix, strlen(f->prefix)) ==
    395       1.1    bouyer 			    0) {
    396       1.1    bouyer 				sys_platform = f->platform;
    397       1.1    bouyer 				break;
    398       1.1    bouyer 			}
    399       1.1    bouyer 
    400       1.1    bouyer 		if (sys_platform == NULL) {
    401       1.1    bouyer 			/*
    402       1.1    bouyer 			 * Early Lemote designs shipped without a model prefix.
    403       1.1    bouyer 			 * Hopefully these well be close enough to the first
    404       1.1    bouyer 			 * generation Fuloong 2F design (LM6002); let's warn
    405       1.1    bouyer 			 * the user and try this if version is 1.2.something
    406       1.1    bouyer 			 * (1.3 onwards are expected to have a model prefix,
    407       1.1    bouyer 			 *  and there are currently no reports of 1.1 and
    408       1.1    bouyer 			 *  below being 2F systems).
    409       1.1    bouyer 			 *
    410       1.1    bouyer 			 * Note that this could be handled by adding a
    411       1.1    bouyer 			 * "1.2." machine type entry to the flavours table,
    412       1.1    bouyer 			 * but I prefer have it stand out.
    413       1.1    bouyer 			 * LM6002 users are encouraged to add the system
    414       1.1    bouyer 			 * model prefix to the `Version' variable.
    415       1.1    bouyer 			 */
    416       1.1    bouyer 			if (strncmp(env, "1.2.", 4) == 0) {
    417       1.1    bouyer 				printf("No model prefix in version"
    418       1.1    bouyer 				    " string \"%s\".\n"
    419       1.1    bouyer 				    "Attempting to match as Lemote Fuloong\n",
    420       1.1    bouyer 				    env);
    421       1.1    bouyer 				sys_platform = &fuloong_platform;
    422       1.1    bouyer 			}
    423       1.1    bouyer 		}
    424       1.1    bouyer 
    425       1.1    bouyer 		if (sys_platform == NULL) {
    426       1.1    bouyer 			printf("This kernel doesn't support model \"%s\"."
    427       1.1    bouyer 			    "\n", env);
    428       1.1    bouyer 			goto unsupported;
    429       1.1    bouyer 		}
    430       1.1    bouyer 	}
    431       1.1    bouyer 
    432       1.6  christos 	cpu_setmodel("%s %s", sys_platform->vendor, sys_platform->product);
    433       1.6  christos 	DPRINTF(("Found %s, setting up.\n", cpu_getmodel()));
    434       1.1    bouyer 
    435       1.1    bouyer 	/*
    436       1.1    bouyer 	 * Figure out memory information.
    437       1.1    bouyer 	 * PMON reports it in two chunks, the memory under the 256MB
    438       1.1    bouyer 	 * CKSEG limit, and memory above that limit.  We need to do the
    439       1.1    bouyer 	 * math ourselves.
    440       1.1    bouyer 	 */
    441       1.1    bouyer 
    442       1.1    bouyer 	env = pmon_getenv("memsize");
    443       1.1    bouyer 	if (env == NULL) {
    444       1.1    bouyer 		printf("Could not get memory information"
    445       1.1    bouyer 		    " from the firmware\n");
    446       1.1    bouyer 		goto unsupported;
    447       1.1    bouyer 	}
    448       1.1    bouyer 	memlo = strtoul(env, NULL, 10);	/* size in MB */
    449       1.1    bouyer 	DPRINTF(("memlo %" PRIdPSIZE, memlo));
    450       1.1    bouyer 	if (memlo < 0 || memlo > 256) {
    451       1.1    bouyer 		printf("Incorrect low memory size `%s'\n", env);
    452       1.1    bouyer 		goto unsupported;
    453       1.1    bouyer 	}
    454       1.1    bouyer 
    455       1.1    bouyer 	/* 3A PMON only reports up to 240MB as low memory */
    456       1.1    bouyer 	if (memlo >= 240) {
    457       1.1    bouyer 		env = pmon_getenv("highmemsize");
    458       1.1    bouyer 		if (env == NULL)
    459       1.1    bouyer 			memhi = 0;
    460       1.1    bouyer 		else
    461       1.1    bouyer 			memhi = strtoul(env, NULL, 10);	/* size in MB */
    462       1.1    bouyer 		if (memhi < 0 || memhi > (64 * 1024) - 256) {
    463       1.1    bouyer 			printf("Incorrect high memory size `%s'\n",
    464       1.1    bouyer 			    env);
    465       1.1    bouyer 			/* better expose the problem than limit to 256MB */
    466       1.1    bouyer 			goto unsupported;
    467       1.1    bouyer 		}
    468       1.1    bouyer 	} else
    469       1.1    bouyer 		memhi = 0;
    470       1.1    bouyer 
    471       1.1    bouyer 	DPRINTF(("memhi %" PRIdPSIZE "\n", memhi));
    472       1.1    bouyer 	memlo = memlo * 1024 * 1024;
    473       1.1    bouyer 	memhi = memhi * 1024 * 1024;
    474       1.1    bouyer 
    475       1.1    bouyer 	switch (loongson_ver) {
    476       1.1    bouyer 	case 0x2e:
    477       1.1    bouyer 		loongson2e_setup(memlo, memhi,
    478       1.1    bouyer 		    MIPS_KSEG0_START, (vaddr_t)kernend, &bonito_dmat);
    479       1.1    bouyer 		break;
    480       1.1    bouyer 	default:
    481       1.1    bouyer 	case 0x2f:
    482       1.1    bouyer 	case 0x3a:
    483       1.1    bouyer 		loongson2f_setup(memlo, memhi,
    484       1.1    bouyer 		    MIPS_KSEG0_START, (vaddr_t)kernend, &bonito_dmat);
    485       1.1    bouyer 		break;
    486       1.1    bouyer 	}
    487       1.1    bouyer 
    488       1.1    bouyer 	DPRINTF(("bonito_pci_init "));
    489       1.1    bouyer 	bonito_pci_init(&bonito_pc, sys_platform->bonito_config);
    490       1.1    bouyer 	bonito_pc.pc_intr_v = __UNCONST(sys_platform->bonito_config);
    491       1.1    bouyer 	bonito_pc.pc_intr_map = loongson_pci_intr_map;
    492       1.1    bouyer 	bonito_pc.pc_intr_string = loongson_pci_intr_string;
    493       1.1    bouyer 	bonito_pc.pc_intr_evcnt = loongson_pci_intr_evcnt;
    494       1.1    bouyer 	bonito_pc.pc_intr_establish = loongson_pci_intr_establish;
    495       1.1    bouyer 	bonito_pc.pc_intr_disestablish = loongson_pci_intr_disestablish;
    496       1.1    bouyer 	bonito_pc.pc_conf_interrupt = loongson_pci_conf_interrupt;
    497       1.1    bouyer 	bonito_pc.pc_pciide_compat_intr_establish =
    498       1.1    bouyer 	    loongson_pciide_compat_intr_establish;
    499       1.1    bouyer 	DPRINTF(("bonito_bus_io_init "));
    500       1.1    bouyer 	bonito_bus_io_init(&bonito_iot, NULL);
    501       1.4    nonaka 	/* override mapping function */
    502       1.4    nonaka 	bonito_iot.bs_map = bonito_bus_io_legacy_map;
    503       1.1    bouyer 	DPRINTF(("bonito_bus_mem_init\n"));
    504       1.1    bouyer 	bonito_bus_mem_init(&bonito_memt, NULL);
    505       1.1    bouyer 
    506       1.1    bouyer 	bonito_dmat._cookie = __UNCONST(sys_platform);
    507       1.1    bouyer 	bonito_dmat._dmamap_ops = mips_bus_dmamap_ops;
    508       1.1    bouyer 	bonito_dmat._dmamem_ops = mips_bus_dmamem_ops;
    509       1.1    bouyer 	bonito_dmat._dmatag_ops = mips_bus_dmatag_ops;
    510       1.1    bouyer 
    511       1.1    bouyer 	DPRINTF(("sys_platform->setup %p\n", sys_platform->setup));
    512       1.1    bouyer 	if (sys_platform->setup != NULL)
    513       1.1    bouyer 		(*(sys_platform->setup))();
    514       1.1    bouyer 
    515       1.1    bouyer #if NCOM > 0
    516       1.1    bouyer 	DPRINTF(("comconsrate %d\n", comconsrate));
    517       1.1    bouyer 	if (comconsrate > 0) {
    518       1.1    bouyer 		if (comcnattach(comconsiot, comconsaddr, comconsrate,
    519       1.1    bouyer 		    COM_FREQ, COM_TYPE_NORMAL,
    520       1.1    bouyer 		    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    521       1.1    bouyer 			panic("unable to initialize serial console");
    522       1.1    bouyer 	}
    523       1.1    bouyer #endif /* NCOM > 0 */
    524       1.1    bouyer 
    525       1.1    bouyer 	for (i = 0; i < 32 - 0x11; i++) {
    526       1.1    bouyer 		pcitag = pci_make_tag(&bonito_pc, 0, i, 0);
    527       1.1    bouyer 		reg = pci_conf_read(&bonito_pc, pcitag, PCI_CLASS_REG);
    528       1.1    bouyer 		DPRINTF(("dev %d class 0x%x", i, reg));
    529       1.1    bouyer 		reg = pci_conf_read(&bonito_pc, pcitag, PCI_ID_REG);
    530       1.1    bouyer 		DPRINTF((" id 0x%x; ", reg));
    531       1.1    bouyer #if NSISFB > 0
    532       1.1    bouyer 		if (cn_tab == &pmoncons)
    533       1.4    nonaka 			sisfb_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
    534       1.4    nonaka 			    pcitag, reg);
    535       1.4    nonaka #endif
    536       1.4    nonaka #if NLYNXFB > 0
    537       1.4    nonaka 		if (cn_tab == &pmoncons)
    538       1.4    nonaka 			lynxfb_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
    539       1.1    bouyer 			    pcitag, reg);
    540       1.1    bouyer #endif
    541       1.3  macallan 		if (cn_tab == &pmoncons)
    542       1.4    nonaka 			gdium_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
    543       1.3  macallan 			    pcitag, reg);
    544       1.4    nonaka 		if (cn_tab != &pmoncons)
    545       1.4    nonaka 			break;
    546       1.1    bouyer 	}
    547       1.4    nonaka #if NPCKBC > 0 || NUKBD > 0
    548       1.4    nonaka 	if (cn_tab != &pmoncons) {
    549       1.4    nonaka 		int rc = ENXIO;
    550       1.4    nonaka #if NPCKBC > 0
    551       1.4    nonaka 		if (rc != 0)
    552       1.5       jdc 			rc = pckbc_cnattach(&bonito_iot, IO_KBD, KBCMDP, 0, 0);
    553       1.4    nonaka #endif
    554       1.4    nonaka #if NUKBD > 0
    555       1.4    nonaka 		if (rc != 0)
    556       1.4    nonaka 			rc = ukbd_cnattach();
    557       1.4    nonaka #endif
    558       1.4    nonaka 	}
    559       1.4    nonaka #endif	/* NPCKBC > 0 || NUKBD > 0 */
    560       1.1    bouyer 	DPRINTF(("\n"));
    561       1.1    bouyer 
    562       1.1    bouyer 	/*
    563       1.1    bouyer 	 * Get the timer from PMON.
    564       1.1    bouyer 	 */
    565       1.1    bouyer 	DPRINTF(("search cpuclock "));
    566       1.1    bouyer 	env = pmon_getenv("cpuclock");
    567       1.1    bouyer 	DPRINTF(("got %s ", env));
    568       1.1    bouyer 	if (env != NULL) {
    569       1.1    bouyer 		curcpu()->ci_cpu_freq =
    570       1.1    bouyer 		    strtoul(env, NULL, 10);
    571       1.1    bouyer 	}
    572       1.1    bouyer 
    573       1.1    bouyer 	DPRINTF(("cpuclock %ld\n", curcpu()->ci_cpu_freq));
    574       1.1    bouyer 
    575       1.1    bouyer 	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    576       1.1    bouyer 		curcpu()->ci_cpu_freq /= 2;
    577       1.1    bouyer 
    578       1.1    bouyer 
    579       1.1    bouyer 	/* Compute the number of ticks for hz. */
    580       1.1    bouyer 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    581       1.1    bouyer 
    582       1.1    bouyer 	/* Compute the delay divisor. */
    583       1.1    bouyer 	curcpu()->ci_divisor_delay =
    584       1.1    bouyer 	    ((curcpu()->ci_cpu_freq + 500000) / 1000000);
    585       1.1    bouyer 
    586       1.1    bouyer 	/*
    587       1.1    bouyer 	 * Get correct cpu frequency if the CPU runs at twice the
    588       1.1    bouyer 	 * external/cp0-count frequency.
    589       1.1    bouyer 	 */
    590       1.1    bouyer 	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    591       1.1    bouyer 		curcpu()->ci_cpu_freq *= 2;
    592       1.1    bouyer 
    593       1.1    bouyer #ifdef DEBUG
    594       1.1    bouyer 	printf("Timer calibration: %lu cycles/sec\n",
    595       1.1    bouyer 	    curcpu()->ci_cpu_freq);
    596       1.1    bouyer #endif
    597       1.1    bouyer 
    598       1.1    bouyer #if NCOM > 0 && 0
    599       1.1    bouyer 	/*
    600       1.1    bouyer 	 * Delay to allow firmware putchars to complete.
    601       1.1    bouyer 	 * FIFO depth * character time.
    602       1.1    bouyer 	 * character time = (1000000 / (defaultrate / 10))
    603       1.1    bouyer 	 */
    604       1.1    bouyer 	delay(160000000 / comcnrate);
    605       1.1    bouyer 	if (comcnattach(&gc->gc_iot, MALTA_UART0ADR, comcnrate,
    606       1.1    bouyer 	    COM_FREQ, COM_TYPE_NORMAL,
    607       1.1    bouyer 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    608       1.1    bouyer 		panic("malta: unable to initialize serial console");
    609       1.1    bouyer #endif /* NCOM > 0 */
    610       1.1    bouyer 
    611       1.1    bouyer 	/*
    612       1.1    bouyer 	 * XXX: check argv[0] - do something if "gdb"???
    613       1.1    bouyer 	 */
    614       1.1    bouyer 
    615       1.1    bouyer 	/*
    616       1.1    bouyer 	 * Look at arguments passed to us and compute boothowto.
    617       1.1    bouyer 	 */
    618       1.1    bouyer 	boothowto = RB_AUTOBOOT;
    619       1.1    bouyer #ifdef NOTYET
    620       1.1    bouyer 	for (i = 1; i < argc; i++) {
    621       1.1    bouyer 		for (cp = argv[i]; *cp; cp++) {
    622       1.1    bouyer 			/* Ignore superfluous '-', if there is one */
    623       1.1    bouyer 			if (*cp == '-')
    624       1.1    bouyer 				continue;
    625       1.1    bouyer 
    626       1.1    bouyer 			howto = 0;
    627       1.1    bouyer 			BOOT_FLAG(*cp, howto);
    628       1.1    bouyer 			if (! howto)
    629       1.1    bouyer 				printf("bootflag '%c' not recognised\n", *cp);
    630       1.1    bouyer 			else
    631       1.1    bouyer 				boothowto |= howto;
    632       1.1    bouyer 		}
    633       1.1    bouyer 	}
    634       1.1    bouyer #endif
    635       1.1    bouyer 
    636       1.1    bouyer 	/*
    637       1.1    bouyer 	 * Load the rest of the available pages into the VM system.
    638       1.1    bouyer 	 */
    639       1.1    bouyer 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
    640       1.1    bouyer 	    mem_clusters, mem_cluster_cnt, NULL, 0);
    641       1.1    bouyer 
    642       1.1    bouyer 	/*
    643       1.1    bouyer 	 * Initialize error message buffer (at end of core).
    644       1.1    bouyer 	 */
    645       1.1    bouyer 	DPRINTF(("mips_init_msgbuf\n"));
    646       1.1    bouyer 	mips_init_msgbuf();
    647       1.1    bouyer 
    648       1.1    bouyer 	DPRINTF(("pmap_bootstrap\n"));
    649       1.1    bouyer 	pmap_bootstrap();
    650       1.1    bouyer 
    651       1.1    bouyer 	/*
    652       1.1    bouyer 	 * Allocate uarea page for lwp0 and set it.
    653       1.1    bouyer 	 */
    654       1.1    bouyer 	DPRINTF(("curlwp %p ", curlwp));
    655       1.1    bouyer 	DPRINTF(("curlwp stack %p\n", curlwp->l_addr));
    656       1.1    bouyer 	mips_init_lwp0_uarea();
    657       1.1    bouyer 
    658       1.1    bouyer 	DPRINTF(("curlwp %p ", curlwp));
    659       1.1    bouyer 	DPRINTF(("curlwp stack %p\n", curlwp->l_addr));
    660       1.1    bouyer 
    661       1.1    bouyer 	/*
    662       1.1    bouyer 	 * Initialize debuggers, and break into them, if appropriate.
    663       1.1    bouyer 	 */
    664       1.1    bouyer #if defined(DDB)
    665       1.1    bouyer 	if (boothowto & RB_KDB)
    666       1.1    bouyer 		Debugger();
    667       1.1    bouyer #endif
    668       1.1    bouyer 	DPRINTF(("return\n"));
    669       1.1    bouyer 	return;
    670       1.1    bouyer unsupported:
    671       1.1    bouyer 	panic("unsupported hardware\n");
    672       1.1    bouyer }
    673       1.1    bouyer 
    674       1.1    bouyer void
    675       1.1    bouyer consinit(void)
    676       1.1    bouyer {
    677       1.1    bouyer 
    678       1.1    bouyer 	/*
    679       1.1    bouyer 	 * Everything related to console initialization is done
    680       1.1    bouyer 	 * in mach_init().
    681       1.1    bouyer 	 */
    682       1.1    bouyer }
    683       1.1    bouyer 
    684       1.1    bouyer /*
    685       1.1    bouyer  * Allocate memory for variable-sized tables,
    686       1.1    bouyer  */
    687       1.1    bouyer void
    688       1.1    bouyer cpu_startup(void)
    689       1.1    bouyer {
    690       1.1    bouyer 	/*
    691       1.1    bouyer 	 *  Do the common startup items.
    692       1.1    bouyer 	 */
    693       1.1    bouyer 	cpu_startup_common();
    694       1.1    bouyer 
    695       1.1    bouyer 	/*
    696       1.1    bouyer 	 * Virtual memory is bootstrapped -- notify the bus spaces
    697       1.1    bouyer 	 * that memory allocation is now safe.
    698       1.1    bouyer 	 */
    699       1.1    bouyer 	ex_mallocsafe = 1;
    700       1.1    bouyer }
    701       1.1    bouyer 
    702       1.1    bouyer int	waittime = -1;
    703       1.1    bouyer 
    704       1.1    bouyer void
    705       1.1    bouyer cpu_reboot(int howto, char *bootstr)
    706       1.1    bouyer {
    707       1.1    bouyer 
    708       1.1    bouyer 	/* Take a snapshot before clobbering any registers. */
    709       1.1    bouyer 	savectx(curpcb);
    710       1.1    bouyer 
    711       1.1    bouyer 	if (cold) {
    712       1.1    bouyer 		howto |= RB_HALT;
    713       1.1    bouyer 		goto haltsys;
    714       1.1    bouyer 	}
    715       1.1    bouyer 
    716       1.1    bouyer 	/* If "always halt" was specified as a boot flag, obey. */
    717       1.1    bouyer 	if (boothowto & RB_HALT)
    718       1.1    bouyer 		howto |= RB_HALT;
    719       1.1    bouyer 
    720       1.1    bouyer 	boothowto = howto;
    721       1.1    bouyer 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    722       1.1    bouyer 		waittime = 0;
    723       1.1    bouyer 		vfs_shutdown();
    724       1.1    bouyer 
    725       1.1    bouyer 		/*
    726       1.1    bouyer 		 * If we've been adjusting the clock, the todr
    727       1.1    bouyer 		 * will be out of synch; adjust it now.
    728       1.1    bouyer 		 */
    729       1.1    bouyer 		resettodr();
    730       1.1    bouyer 	}
    731       1.1    bouyer 
    732       1.1    bouyer 	splhigh();
    733       1.1    bouyer 
    734       1.1    bouyer 	if (howto & RB_DUMP)
    735       1.1    bouyer 		dumpsys();
    736       1.1    bouyer 
    737       1.1    bouyer haltsys:
    738       1.1    bouyer 	doshutdownhooks();
    739       1.1    bouyer 
    740       1.1    bouyer 	pmf_system_shutdown(boothowto);
    741       1.1    bouyer 
    742       1.1    bouyer 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    743       1.1    bouyer 		if (sys_platform->powerdown != NULL)
    744       1.1    bouyer 			sys_platform->powerdown();
    745       1.1    bouyer 	}
    746       1.1    bouyer 
    747       1.1    bouyer 	if (howto & RB_HALT) {
    748       1.1    bouyer 		printf("\n");
    749       1.1    bouyer 		printf("The operating system has halted.\n");
    750       1.1    bouyer 		printf("Please press any key to reboot.\n\n");
    751       1.1    bouyer 		cnpollc(1);	/* For proper keyboard command handling */
    752       1.1    bouyer 		cngetc();
    753       1.1    bouyer 		cnpollc(0);
    754       1.1    bouyer 	}
    755       1.1    bouyer 
    756       1.1    bouyer 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    757       1.1    bouyer 
    758       1.1    bouyer 	if (sys_platform->reset != NULL)
    759       1.1    bouyer 		sys_platform->reset();
    760       1.1    bouyer 
    761       1.1    bouyer 	__asm__ __volatile__ (
    762       1.1    bouyer 		"\t.long 0x3c02bfc0\n"
    763       1.1    bouyer 		"\t.long 0x00400008\n"
    764       1.1    bouyer 	    ::: "v0");
    765       1.1    bouyer }
    766       1.1    bouyer 
    767       1.1    bouyer /*
    768       1.1    bouyer  * Early console through pmon routines.
    769       1.1    bouyer  */
    770       1.1    bouyer 
    771       1.1    bouyer int
    772       1.1    bouyer pmoncngetc(dev_t dev)
    773       1.1    bouyer {
    774       1.1    bouyer 	/*
    775       1.1    bouyer 	 * PMON does not give us a getc routine.  So try to get a whole line
    776       1.1    bouyer 	 * and return it char by char, trying not to lose the \n.  Kind
    777       1.1    bouyer 	 * of ugly but should work.
    778       1.1    bouyer 	 *
    779       1.1    bouyer 	 * Note that one could theoretically use pmon_read(STDIN, &c, 1)
    780       1.1    bouyer 	 * but the value of STDIN within PMON is not a constant and there
    781       1.1    bouyer 	 * does not seem to be a way of letting us know which value to use.
    782       1.1    bouyer 	 */
    783       1.1    bouyer 	static char buf[1 + PMON_MAXLN];
    784       1.1    bouyer 	static char *bufpos = buf;
    785       1.1    bouyer 	int c;
    786       1.1    bouyer 
    787       1.1    bouyer 	if (*bufpos == '\0') {
    788       1.1    bouyer 		bufpos = buf;
    789       1.1    bouyer 		if (pmon_gets(buf) == NULL) {
    790       1.1    bouyer 			/* either an empty line or EOF. assume the former */
    791       1.1    bouyer 			return (int)'\n';
    792       1.1    bouyer 		} else {
    793       1.1    bouyer 			/* put back the \n sign */
    794       1.1    bouyer 			buf[strlen(buf)] = '\n';
    795       1.1    bouyer 		}
    796       1.1    bouyer 	}
    797       1.1    bouyer 
    798       1.1    bouyer 	c = (int)*bufpos++;
    799       1.1    bouyer 	if (bufpos - buf > PMON_MAXLN) {
    800       1.1    bouyer 		bufpos = buf;
    801       1.1    bouyer 		*bufpos = '\0';
    802       1.1    bouyer 	}
    803       1.1    bouyer 
    804       1.1    bouyer 	return c;
    805       1.1    bouyer }
    806       1.1    bouyer 
    807       1.1    bouyer void
    808       1.1    bouyer pmoncnputc(dev_t dev, int c)
    809       1.1    bouyer {
    810       1.1    bouyer 	if (c == '\n')
    811       1.1    bouyer 		pmon_printf("\n");
    812       1.1    bouyer 	else
    813       1.1    bouyer 		pmon_printf("%c", c);
    814       1.1    bouyer }
    815