Index: head/sys/boot/common/bootstrap.h =================================================================== --- head/sys/boot/common/bootstrap.h (revision 307323) +++ head/sys/boot/common/bootstrap.h (revision 307324) @@ -1,335 +1,333 @@ /*- * Copyright (c) 1998 Michael Smith * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _BOOTSTRAP_H_ #define _BOOTSTRAP_H_ #include #include #include /* Commands and return values; nonzero return sets command_errmsg != NULL */ typedef int (bootblk_cmd_t)(int argc, char *argv[]); #define COMMAND_ERRBUFSZ (256) extern char *command_errmsg; extern char command_errbuf[COMMAND_ERRBUFSZ]; #define CMD_OK 0 #define CMD_WARN 1 #define CMD_ERROR 2 #define CMD_CRIT 3 #define CMD_FATAL 4 /* interp.c */ void interact(const char *rc); int include(const char *filename); /* interp_backslash.c */ char *backslash(char *str); /* interp_parse.c */ int parse(int *argc, char ***argv, char *str); /* interp_forth.c */ void bf_init(const char *rc); int bf_run(char *line); /* boot.c */ int autoboot(int timeout, char *prompt); void autoboot_maybe(void); int getrootmount(char *rootdev); /* misc.c */ char *unargv(int argc, char *argv[]); void hexdump(caddr_t region, size_t len); size_t strlenout(vm_offset_t str); char *strdupout(vm_offset_t str); void kern_bzero(vm_offset_t dest, size_t len); int kern_pread(int fd, vm_offset_t dest, size_t len, off_t off); void *alloc_pread(int fd, off_t off, size_t len); /* bcache.c */ void bcache_init(u_int nblks, size_t bsize); void bcache_add_dev(int); void *bcache_allocate(void); void bcache_free(void *); int bcache_strategy(void *devdata, int rw, daddr_t blk, size_t offset, size_t size, char *buf, size_t *rsize); /* * Disk block cache */ struct bcache_devdata { int (*dv_strategy)(void *devdata, int rw, daddr_t blk, size_t offset, size_t size, char *buf, size_t *rsize); void *dv_devdata; void *dv_cache; }; /* * Modular console support. */ struct console { const char *c_name; const char *c_desc; int c_flags; #define C_PRESENTIN (1<<0) /* console can provide input */ #define C_PRESENTOUT (1<<1) /* console can provide output */ #define C_ACTIVEIN (1<<2) /* user wants input from console */ #define C_ACTIVEOUT (1<<3) /* user wants output to console */ #define C_WIDEOUT (1<<4) /* c_out routine groks wide chars */ void (* c_probe)(struct console *cp); /* set c_flags to match hardware */ int (* c_init)(int arg); /* reinit XXX may need more args */ void (* c_out)(int c); /* emit c */ int (* c_in)(void); /* wait for and return input */ int (* c_ready)(void); /* return nonzer if input waiting */ }; extern struct console *consoles[]; void cons_probe(void); /* * Plug-and-play enumerator/configurator interface. */ struct pnphandler { const char *pp_name; /* handler/bus name */ void (* pp_enumerate)(void); /* enumerate PnP devices, add to chain */ }; struct pnpident { char *id_ident; /* ASCII identifier, actual format varies with bus/handler */ STAILQ_ENTRY(pnpident) id_link; }; struct pnpinfo { char *pi_desc; /* ASCII description, optional */ int pi_revision; /* optional revision (or -1) if not supported */ char *pi_module; /* module/args nominated to handle device */ int pi_argc; /* module arguments */ char **pi_argv; struct pnphandler *pi_handler; /* handler which detected this device */ STAILQ_HEAD(,pnpident) pi_ident; /* list of identifiers */ STAILQ_ENTRY(pnpinfo) pi_link; }; STAILQ_HEAD(pnpinfo_stql, pnpinfo); -extern struct pnpinfo_stql pnp_devices; - extern struct pnphandler *pnphandlers[]; /* provided by MD code */ void pnp_addident(struct pnpinfo *pi, char *ident); struct pnpinfo *pnp_allocinfo(void); void pnp_freeinfo(struct pnpinfo *pi); void pnp_addinfo(struct pnpinfo *pi); char *pnp_eisaformat(u_int8_t *data); /* * < 0 - No ISA in system * == 0 - Maybe ISA, search for read data port * > 0 - ISA in system, value is read data port address */ extern int isapnp_readport; /* * Preloaded file metadata header. * * Metadata are allocated on our heap, and copied into kernel space * before executing the kernel. */ struct file_metadata { size_t md_size; u_int16_t md_type; struct file_metadata *md_next; char md_data[1]; /* data are immediately appended */ }; struct preloaded_file; struct mod_depend; struct kernel_module { char *m_name; /* module name */ int m_version; /* module version */ /* char *m_args;*/ /* arguments for the module */ struct preloaded_file *m_fp; struct kernel_module *m_next; }; /* * Preloaded file information. Depending on type, file can contain * additional units called 'modules'. * * At least one file (the kernel) must be loaded in order to boot. * The kernel is always loaded first. * * String fields (m_name, m_type) should be dynamically allocated. */ struct preloaded_file { char *f_name; /* file name */ char *f_type; /* verbose file type, eg 'ELF kernel', 'pnptable', etc. */ char *f_args; /* arguments for the file */ struct file_metadata *f_metadata; /* metadata that will be placed in the module directory */ int f_loader; /* index of the loader that read the file */ vm_offset_t f_addr; /* load address */ size_t f_size; /* file size */ struct kernel_module *f_modules; /* list of modules if any */ struct preloaded_file *f_next; /* next file */ }; struct file_format { /* Load function must return EFTYPE if it can't handle the module supplied */ int (* l_load)(char *filename, u_int64_t dest, struct preloaded_file **result); /* Only a loader that will load a kernel (first module) should have an exec handler */ int (* l_exec)(struct preloaded_file *mp); }; extern struct file_format *file_formats[]; /* supplied by consumer */ extern struct preloaded_file *preloaded_files; int mod_load(char *name, struct mod_depend *verinfo, int argc, char *argv[]); int mod_loadkld(const char *name, int argc, char *argv[]); void unload(void); struct preloaded_file *file_alloc(void); struct preloaded_file *file_findfile(const char *name, const char *type); struct file_metadata *file_findmetadata(struct preloaded_file *fp, int type); struct preloaded_file *file_loadraw(const char *name, char *type, int insert); void file_discard(struct preloaded_file *fp); void file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p); int file_addmodule(struct preloaded_file *fp, char *modname, int version, struct kernel_module **newmp); /* MI module loaders */ #ifdef __elfN /* Relocation types. */ #define ELF_RELOC_REL 1 #define ELF_RELOC_RELA 2 /* Relocation offset for some architectures */ extern u_int64_t __elfN(relocation_offset); struct elf_file; typedef Elf_Addr (symaddr_fn)(struct elf_file *ef, Elf_Size symidx); int __elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result); int __elfN(obj_loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result); int __elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr, const void *reldata, int reltype, Elf_Addr relbase, Elf_Addr dataaddr, void *data, size_t len); int __elfN(loadfile_raw)(char *filename, u_int64_t dest, struct preloaded_file **result, int multiboot); int __elfN(load_modmetadata)(struct preloaded_file *fp, u_int64_t dest); #endif /* * Support for commands */ struct bootblk_command { const char *c_name; const char *c_desc; bootblk_cmd_t *c_fn; }; #define COMMAND_SET(tag, key, desc, func) \ static bootblk_cmd_t func; \ static struct bootblk_command _cmd_ ## tag = { key, desc, func }; \ DATA_SET(Xcommand_set, _cmd_ ## tag) SET_DECLARE(Xcommand_set, struct bootblk_command); /* * The intention of the architecture switch is to provide a convenient * encapsulation of the interface between the bootstrap MI and MD code. * MD code may selectively populate the switch at runtime based on the * actual configuration of the target system. */ struct arch_switch { /* Automatically load modules as required by detected hardware */ int (*arch_autoload)(void); /* Locate the device for (name), return pointer to tail in (*path) */ int (*arch_getdev)(void **dev, const char *name, const char **path); /* Copy from local address space to module address space, similar to bcopy() */ ssize_t (*arch_copyin)(const void *src, vm_offset_t dest, const size_t len); /* Copy to local address space from module address space, similar to bcopy() */ ssize_t (*arch_copyout)(const vm_offset_t src, void *dest, const size_t len); /* Read from file to module address space, same semantics as read() */ ssize_t (*arch_readin)(const int fd, vm_offset_t dest, const size_t len); /* Perform ISA byte port I/O (only for systems with ISA) */ int (*arch_isainb)(int port); void (*arch_isaoutb)(int port, int value); /* * Interface to adjust the load address according to the "object" * being loaded. */ uint64_t (*arch_loadaddr)(u_int type, void *data, uint64_t addr); #define LOAD_ELF 1 /* data points to the ELF header. */ #define LOAD_RAW 2 /* data points to the file name. */ /* * Interface to inform MD code about a loaded (ELF) segment. This * can be used to flush caches and/or set up translations. */ #ifdef __elfN void (*arch_loadseg)(Elf_Ehdr *eh, Elf_Phdr *ph, uint64_t delta); #else void (*arch_loadseg)(void *eh, void *ph, uint64_t delta); #endif /* Probe ZFS pool(s), if needed. */ void (*arch_zfs_probe)(void); }; extern struct arch_switch archsw; /* This must be provided by the MD code, but should it be in the archsw? */ void delay(int delay); void dev_cleanup(void); time_t time(time_t *tloc); #ifndef CTASSERT /* Allow lint to override */ #define CTASSERT(x) _CTASSERT(x, __LINE__) #define _CTASSERT(x, y) __CTASSERT(x, y) #define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1] #endif #endif /* !_BOOTSTRAP_H_ */ Index: head/sys/boot/common/pnp.c =================================================================== --- head/sys/boot/common/pnp.c (revision 307323) +++ head/sys/boot/common/pnp.c (revision 307324) @@ -1,187 +1,232 @@ /* * mjs copyright * */ #include __FBSDID("$FreeBSD$"); /* * "Plug and Play" functionality. * * We use the PnP enumerators to obtain identifiers for installed hardware, * and the contents of a database to determine modules to be loaded to support * such hardware. */ #include #include #include +#include "ficl.h" -struct pnpinfo_stql pnp_devices; +static struct pnpinfo_stql pnp_devices; static int pnp_devices_initted = 0; static void pnp_discard(void); /* * Perform complete enumeration sweep */ COMMAND_SET(pnpscan, "pnpscan", "scan for PnP devices", pnp_scan); static int pnp_scan(int argc, char *argv[]) { struct pnpinfo *pi; int hdlr; int verbose; int ch; if (pnp_devices_initted == 0) { STAILQ_INIT(&pnp_devices); pnp_devices_initted = 1; } verbose = 0; optind = 1; optreset = 1; while ((ch = getopt(argc, argv, "v")) != -1) { switch(ch) { case 'v': verbose = 1; break; case '?': default: /* getopt has already reported an error */ return(CMD_OK); } } /* forget anything we think we knew */ pnp_discard(); /* iterate over all of the handlers */ for (hdlr = 0; pnphandlers[hdlr] != NULL; hdlr++) { if (verbose) printf("Probing %s...\n", pnphandlers[hdlr]->pp_name); pnphandlers[hdlr]->pp_enumerate(); } if (verbose) { pager_open(); if (pager_output("PNP scan summary:\n")) goto out; STAILQ_FOREACH(pi, &pnp_devices, pi_link) { pager_output(STAILQ_FIRST(&pi->pi_ident)->id_ident); /* first ident should be canonical */ if (pi->pi_desc != NULL) { pager_output(" : "); pager_output(pi->pi_desc); } if (pager_output("\n")) break; } out: pager_close(); } return(CMD_OK); } /* * Throw away anything we think we know about PnP devices. */ static void pnp_discard(void) { struct pnpinfo *pi; while (STAILQ_FIRST(&pnp_devices) != NULL) { pi = STAILQ_FIRST(&pnp_devices); STAILQ_REMOVE_HEAD(&pnp_devices, pi_link); pnp_freeinfo(pi); } } /* * Add a unique identifier to (pi) */ void pnp_addident(struct pnpinfo *pi, char *ident) { struct pnpident *id; STAILQ_FOREACH(id, &pi->pi_ident, id_link) if (!strcmp(id->id_ident, ident)) return; /* already have this one */ id = malloc(sizeof(struct pnpident)); id->id_ident = strdup(ident); STAILQ_INSERT_TAIL(&pi->pi_ident, id, id_link); } /* * Allocate a new pnpinfo struct */ struct pnpinfo * pnp_allocinfo(void) { struct pnpinfo *pi; pi = malloc(sizeof(struct pnpinfo)); bzero(pi, sizeof(struct pnpinfo)); STAILQ_INIT(&pi->pi_ident); return(pi); } /* * Release storage held by a pnpinfo struct */ void pnp_freeinfo(struct pnpinfo *pi) { struct pnpident *id; while (!STAILQ_EMPTY(&pi->pi_ident)) { id = STAILQ_FIRST(&pi->pi_ident); STAILQ_REMOVE_HEAD(&pi->pi_ident, id_link); free(id->id_ident); free(id); } if (pi->pi_desc) free(pi->pi_desc); if (pi->pi_module) free(pi->pi_module); if (pi->pi_argv) free(pi->pi_argv); free(pi); } /* * Add a new pnpinfo struct to the list. */ void pnp_addinfo(struct pnpinfo *pi) { STAILQ_INSERT_TAIL(&pnp_devices, pi, pi_link); } /* * Format an EISA id as a string in standard ISA PnP format, AAAIIRR * where 'AAA' is the EISA vendor ID, II is the product ID and RR the revision ID. */ char * pnp_eisaformat(u_int8_t *data) { static char idbuf[8]; const char hextoascii[] = "0123456789abcdef"; idbuf[0] = '@' + ((data[0] & 0x7c) >> 2); idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5)); idbuf[2] = '@' + (data[1] & 0x1f); idbuf[3] = hextoascii[(data[2] >> 4)]; idbuf[4] = hextoascii[(data[2] & 0xf)]; idbuf[5] = hextoascii[(data[3] >> 4)]; idbuf[6] = hextoascii[(data[3] & 0xf)]; idbuf[7] = 0; return(idbuf); } +void +ficlPnpdevices(FICL_VM *pVM) +{ + static int pnp_devices_initted = 0; +#if FICL_ROBUST > 1 + vmCheckStack(pVM, 0, 1); +#endif + + if(!pnp_devices_initted) { + STAILQ_INIT(&pnp_devices); + pnp_devices_initted = 1; + } + + stackPushPtr(pVM->pStack, &pnp_devices); + + return; +} + +void +ficlPnphandlers(FICL_VM *pVM) +{ +#if FICL_ROBUST > 1 + vmCheckStack(pVM, 0, 1); +#endif + + stackPushPtr(pVM->pStack, pnphandlers); + + return; +} + +/* + * Glue function to add the appropriate forth words to access pnp BIOS + * functionality. + */ +static void ficlCompilePnp(FICL_SYSTEM *pSys) +{ + FICL_DICT *dp = pSys->dp; + assert (dp); + + dictAppendWord(dp, "pnpdevices",ficlPnpdevices, FW_DEFAULT); + dictAppendWord(dp, "pnphandlers",ficlPnphandlers, FW_DEFAULT); +} + +FICL_COMPILE_SET(ficlCompilePnp); Index: head/sys/boot/efi/loader/Makefile =================================================================== --- head/sys/boot/efi/loader/Makefile (revision 307323) +++ head/sys/boot/efi/loader/Makefile (revision 307324) @@ -1,171 +1,172 @@ # $FreeBSD$ MAN= .include MK_SSP= no PROG= loader.sym INTERNALPROG= WARNS?= 3 # architecture-specific loader code SRCS= autoload.c \ bootinfo.c \ conf.c \ copy.c \ devicename.c \ main.c \ self_reloc.c \ smbios.c \ vers.c .if ${MK_ZFS} != "no" SRCS+= zfs.c .PATH: ${.CURDIR}/../../zfs SRCS+= skein.c skein_block.c # Do not unroll skein loops, reduce code size CFLAGS+= -DSKEIN_LOOP=111 .PATH: ${.CURDIR}/../../../crypto/skein # Disable warnings that are currently incompatible with the zfs boot code CWARNFLAGS.zfs.c+= -Wno-sign-compare CWARNFLAGS.zfs.c+= -Wno-array-bounds CWARNFLAGS.zfs.c+= -Wno-missing-prototypes .endif # We implement a slightly non-standard %S in that it always takes a # CHAR16 that's common in UEFI-land instead of a wchar_t. This only # seems to matter on arm64 where wchar_t defaults to an int instead # of a short. There's no good cast to use here so just ignore the # warnings for now. CWARNFLAGS.main.c+= -Wno-format .PATH: ${.CURDIR}/arch/${MACHINE} # For smbios.c .PATH: ${.CURDIR}/../../i386/libi386 .include "${.CURDIR}/arch/${MACHINE}/Makefile.inc" CFLAGS+= -I${.CURDIR} CFLAGS+= -I${.CURDIR}/arch/${MACHINE} CFLAGS+= -I${.CURDIR}/../include CFLAGS+= -I${.CURDIR}/../include/${MACHINE} CFLAGS+= -I${.CURDIR}/../../../contrib/dev/acpica/include CFLAGS+= -I${.CURDIR}/../../.. CFLAGS+= -I${.CURDIR}/../../i386/libi386 .if ${MK_ZFS} != "no" CFLAGS+= -I${.CURDIR}/../../zfs CFLAGS+= -I${.CURDIR}/../../../cddl/boot/zfs CFLAGS+= -I${.CURDIR}/../../../crypto/skein CFLAGS+= -DEFI_ZFS_BOOT .endif CFLAGS+= -DNO_PCI -DEFI # make buildenv doesn't set DESTDIR, this means LIBSTAND # will be wrong when crossbuilding. .if exists(${.OBJDIR}/../../../../lib/libstand/libstand.a) LIBSTAND= ${.OBJDIR}/../../../../lib/libstand/libstand.a .endif .if !defined(BOOT_HIDE_SERIAL_NUMBERS) # Export serial numbers, UUID, and asset tag from loader. CFLAGS+= -DSMBIOS_SERIAL_NUMBERS .if defined(BOOT_LITTLE_ENDIAN_UUID) # Use little-endian UUID format as defined in SMBIOS 2.6. CFLAGS+= -DSMBIOS_LITTLE_ENDIAN_UUID .elif defined(BOOT_NETWORK_ENDIAN_UUID) # Use network-endian UUID format for backward compatibility. CFLAGS+= -DSMBIOS_NETWORK_ENDIAN_UUID .endif .endif .if ${MK_FORTH} != "no" BOOT_FORTH= yes CFLAGS+= -DBOOT_FORTH CFLAGS+= -I${.CURDIR}/../../ficl CFLAGS+= -I${.CURDIR}/../../ficl/${MACHINE_CPUARCH} LIBFICL= ${.OBJDIR}/../../ficl/libficl.a .endif LOADER_FDT_SUPPORT?= no .if ${MK_FDT} != "no" && ${LOADER_FDT_SUPPORT} != "no" CFLAGS+= -I${.CURDIR}/../../fdt CFLAGS+= -I${.OBJDIR}/../../fdt CFLAGS+= -DLOADER_FDT_SUPPORT LIBEFI_FDT= ${.OBJDIR}/../../efi/fdt/libefi_fdt.a LIBFDT= ${.OBJDIR}/../../fdt/libfdt.a .endif # Include bcache code. HAVE_BCACHE= yes .if defined(EFI_STAGING_SIZE) CFLAGS+= -DEFI_STAGING_SIZE=${EFI_STAGING_SIZE} .endif # Always add MI sources .PATH: ${.CURDIR}/../../common .include "${.CURDIR}/../../common/Makefile.inc" CFLAGS+= -I${.CURDIR}/../../common FILES+= loader.efi FILESMODE_loader.efi= ${BINMODE} LDSCRIPT= ${.CURDIR}/arch/${MACHINE}/ldscript.${MACHINE} LDFLAGS+= -Wl,-T${LDSCRIPT} -Wl,-Bsymbolic -shared CLEANFILES+= vers.c loader.efi NEWVERSWHAT= "EFI loader" ${MACHINE} vers.c: ${.CURDIR}/../../common/newvers.sh ${.CURDIR}/../../efi/loader/version sh ${.CURDIR}/../../common/newvers.sh ${.CURDIR}/version ${NEWVERSWHAT} NM?= nm OBJCOPY?= objcopy .if ${MACHINE_CPUARCH} == "amd64" EFI_TARGET= efi-app-x86_64 .elif ${MACHINE_CPUARCH} == "i386" EFI_TARGET= efi-app-ia32 .else EFI_TARGET= binary .endif # Arbitrarily set the PE/COFF header timestamps to 1 Jan 2016 00:00:00 # for build reproducibility. SOURCE_DATE_EPOCH?=1451606400 loader.efi: ${PROG} if ${NM} ${.ALLSRC} | grep ' U '; then \ echo "Undefined symbols in ${.ALLSRC}"; \ exit 1; \ fi SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH} \ ${OBJCOPY} -j .peheader -j .text -j .sdata -j .data \ -j .dynamic -j .dynsym -j .rel.dyn \ -j .rela.dyn -j .reloc -j .eh_frame -j set_Xcommand_set \ + -j set_Xficl_compile_set \ --output-target=${EFI_TARGET} ${.ALLSRC} ${.TARGET} LIBEFI= ${.OBJDIR}/../libefi/libefi.a DPADD= ${LIBFICL} ${LIBEFI} ${LIBFDT} ${LIBEFI_FDT} ${LIBSTAND} \ ${LDSCRIPT} LDADD= ${LIBFICL} ${LIBEFI} ${LIBFDT} ${LIBEFI_FDT} ${LIBSTAND} .include beforedepend ${OBJS}: machine CLEANFILES+= machine machine: .NOMETA ln -sf ${.CURDIR}/../../../${MACHINE}/include machine .if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386" beforedepend ${OBJS}: x86 CLEANFILES+= x86 x86: .NOMETA ln -sf ${.CURDIR}/../../../x86/include x86 .endif Index: head/sys/boot/efi/loader/arch/amd64/ldscript.amd64 =================================================================== --- head/sys/boot/efi/loader/arch/amd64/ldscript.amd64 (revision 307323) +++ head/sys/boot/efi/loader/arch/amd64/ldscript.amd64 (revision 307324) @@ -1,67 +1,72 @@ /* $FreeBSD$ */ OUTPUT_FORMAT("elf64-x86-64-freebsd", "elf64-x86-64-freebsd", "elf64-x86-64-freebsd") OUTPUT_ARCH(i386:x86-64) ENTRY(_start) SECTIONS { /* Read-only sections, merged into text segment: */ . = 0; ImageBase = .; .hash : { *(.hash) } /* this MUST come first! */ . = ALIGN(4096); .eh_frame : { *(.eh_frame) } . = ALIGN(4096); .text : { *(.text .stub .text.* .gnu.linkonce.t.*) /* .gnu.warning sections are handled specially by elf32.em. */ *(.gnu.warning) *(.plt) } =0xCCCCCCCC . = ALIGN(4096); .data : { *(.rodata .rodata.* .gnu.linkonce.r.*) *(.rodata1) *(.sdata2 .sdata2.* .gnu.linkonce.s2.*) *(.sbss2 .sbss2.* .gnu.linkonce.sb2.*) *(.opd) *(.data .data.* .gnu.linkonce.d.*) *(.data1) *(.plabel) *(.dynbss) *(.bss .bss.* .gnu.linkonce.b.*) *(COMMON) } . = ALIGN(4096); set_Xcommand_set : { __start_set_Xcommand_set = .; *(set_Xcommand_set) __stop_set_Xcommand_set = .; } + set_Xficl_compile_set : { + __start_set_Xficl_compile_set = .; + *(set_Xficl_compile_set) + __stop_set_Xficl_compile_set = .; + } . = ALIGN(4096); __gp = .; .sdata : { *(.got.plt .got) *(.sdata .sdata.* .gnu.linkonce.s.*) *(dynsbss) *(.sbss .sbss.* .gnu.linkonce.sb.*) *(.scommon) } . = ALIGN(4096); .dynamic : { *(.dynamic) } . = ALIGN(4096); .rela.dyn : { *(.rela.data*) *(.rela.got) *(.rela.stab) *(.relaset_*) } . = ALIGN(4096); .reloc : { *(.reloc) } . = ALIGN(4096); .dynsym : { *(.dynsym) } . = ALIGN(4096); .dynstr : { *(.dynstr) } } Index: head/sys/boot/efi/loader/arch/arm/ldscript.arm =================================================================== --- head/sys/boot/efi/loader/arch/arm/ldscript.arm (revision 307323) +++ head/sys/boot/efi/loader/arch/arm/ldscript.arm (revision 307324) @@ -1,62 +1,67 @@ /* $FreeBSD$ */ OUTPUT_ARCH(arm) ENTRY(_start) SECTIONS { /* Read-only sections, merged into text segment: */ . = 0; ImageBase = .; .text : { *(.peheader) *(.text .stub .text.* .gnu.linkonce.t.*) /* .gnu.warning sections are handled specially by elf32.em. */ *(.gnu.warning) *(.gnu.linkonce.t*) } =0 _etext = .; PROVIDE (etext = .); . = ALIGN(16); .data : { *(.data *.data.*) *(.gnu.linkonce.d*) *(.rodata) *(.rodata.*) CONSTRUCTORS . = ALIGN(4); PROVIDE (__bss_start = .); *(.sbss) *(.scommon) *(.dynsbss) *(.dynbss) *(.bss) *(COMMON) . = ALIGN(4); PROVIDE (__bss_end = .); } /* We want the small data sections together, so single-instruction offsets can access them all, and initialized data all before uninitialized, so we can shorten the on-disk segment size. */ .sdata : { *(.got.plt .got) *(.sdata*.sdata.* .gnu.linkonce.s.*) } set_Xcommand_set : { __start_set_Xcommand_set = .; *(set_Xcommand_set) __stop_set_Xcommand_set = .; } + set_Xficl_compile_set : { + __start_set_Xficl_compile_set = .; + *(set_Xficl_compile_set) + __stop_set_Xficl_compile_set = .; + } __gp = .; .plt : { *(.plt) } .dynamic : { *(.dynamic) } .reloc : { *(.reloc) } .dynsym : { *(.dynsym) } .dynstr : { *(.dynstr) } .rel.dyn : { *(.rel.*) *(.relset_*) } _edata = .; .hash : { *(.hash) } } Index: head/sys/boot/efi/loader/arch/arm64/ldscript.arm64 =================================================================== --- head/sys/boot/efi/loader/arch/arm64/ldscript.arm64 (revision 307323) +++ head/sys/boot/efi/loader/arch/arm64/ldscript.arm64 (revision 307324) @@ -1,80 +1,85 @@ /* $FreeBSD$ */ /* OUTPUT_FORMAT("elf64-aarch64-freebsd", "elf64-aarch64-freebsd", "elf64-aarch64-freebsd") */ OUTPUT_ARCH(aarch64) ENTRY(_start) SECTIONS { /* Read-only sections, merged into text segment: */ . = 0; ImageBase = .; .text : { *(.peheader) *(.text .stub .text.* .gnu.linkonce.t.*) /* .gnu.warning sections are handled specially by elf32.em. */ *(.gnu.warning) *(.plt) } =0xD4200000 . = ALIGN(16); .data : { *(.rodata .rodata.* .gnu.linkonce.r.*) *(.rodata1) *(.sdata2 .sdata2.* .gnu.linkonce.s2.*) *(.sbss2 .sbss2.* .gnu.linkonce.sb2.*) *(.opd) *(.data .data.* .gnu.linkonce.d.*) *(.data1) *(.plabel) . = ALIGN(16); __bss_start = .; *(.sbss .sbss.* .gnu.linkonce.sb.*) *(.scommon) *(.dynbss) *(.bss *.bss.*) *(COMMON) . = ALIGN(16); __bss_end = .; } . = ALIGN(16); set_Xcommand_set : { __start_set_Xcommand_set = .; *(set_Xcommand_set) __stop_set_Xcommand_set = .; } + set_Xficl_compile_set : { + __start_set_Xficl_compile_set = .; + *(set_Xficl_compile_set) + __stop_set_Xficl_compile_set = .; + } . = ALIGN(16); __gp = .; .sdata : { *(.got.plt .got) *(.sdata .sdata.* .gnu.linkonce.s.*) *(dynsbss) *(.scommon) } . = ALIGN(16); .dynamic : { *(.dynamic) } . = ALIGN(16); .rela.dyn : { *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) *(.rela.got) *(.rela.sdata .rela.sdata.* .rela.gnu.linkonce.s.*) *(.rela.sbss .rela.sbss.* .rela.gnu.linkonce.sb.*) *(.rela.sdata2 .rela.sdata2.* .rela.gnu.linkonce.s2.*) *(.rela.sbss2 .rela.sbss2.* .rela.gnu.linkonce.sb2.*) *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) *(.rela.plt) *(.relset_*) *(.rela.dyn .rela.dyn.*) } . = ALIGN(16); .reloc : { *(.reloc) } . = ALIGN(16); .dynsym : { *(.dynsym) } _edata = .; /* Unused sections */ .dynstr : { *(.dynstr) } .hash : { *(.hash) } } Index: head/sys/boot/efi/loader/arch/i386/ldscript.i386 =================================================================== --- head/sys/boot/efi/loader/arch/i386/ldscript.i386 (revision 307323) +++ head/sys/boot/efi/loader/arch/i386/ldscript.i386 (revision 307324) @@ -1,72 +1,77 @@ /* $FreeBSD$ */ OUTPUT_FORMAT("elf32-i386-freebsd", "elf32-i386-freebsd", "elf32-i386-freebsd") OUTPUT_ARCH(i386) ENTRY(_start) SECTIONS { /* Read-only sections, merged into text segment: */ . = 0; ImageBase = .; . = SIZEOF_HEADERS; . = ALIGN(4096); .text : { *(.text .stub .text.* .gnu.linkonce.t.*) /* .gnu.warning sections are handled specially by elf32.em. */ *(.gnu.warning) *(.plt) } =0xCCCCCCCC . = ALIGN(4096); .data : { *(.rodata .rodata.* .gnu.linkonce.r.*) *(.rodata1) *(.sdata2 .sdata2.* .gnu.linkonce.s2.*) *(.sbss2 .sbss2.* .gnu.linkonce.sb2.*) *(.opd) *(.data .data.* .gnu.linkonce.d.*) *(.data1) *(.plabel) *(.dynbss) *(.bss .bss.* .gnu.linkonce.b.*) *(COMMON) } . = ALIGN(4096); set_Xcommand_set : { __start_set_Xcommand_set = .; *(set_Xcommand_set) __stop_set_Xcommand_set = .; } + set_Xficl_compile_set : { + __start_set_Xficl_compile_set = .; + *(set_Xficl_compile_set) + __stop_set_Xficl_compile_set = .; + } . = ALIGN(4096); __gp = .; .sdata : { *(.got.plt .got) *(.sdata .sdata.* .gnu.linkonce.s.*) *(dynsbss) *(.sbss .sbss.* .gnu.linkonce.sb.*) *(.scommon) } . = ALIGN(4096); .dynamic : { *(.dynamic) } . = ALIGN(4096); .rel.dyn : { *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*) *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*) *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*) *(.rel.got) *(.rel.sdata .rel.sdata.* .rel.gnu.linkonce.s.*) *(.rel.sbss .rel.sbss.* .rel.gnu.linkonce.sb.*) *(.rel.sdata2 .rel.sdata2.* .rel.gnu.linkonce.s2.*) *(.rel.sbss2 .rel.sbss2.* .rel.gnu.linkonce.sb2.*) *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*) *(.rel.plt) *(.relset_*) *(.rel.dyn .rel.dyn.*) } . = ALIGN(4096); .reloc : { *(.reloc) } . = ALIGN(4096); .hash : { *(.hash) } . = ALIGN(4096); .dynsym : { *(.dynsym) } . = ALIGN(4096); .dynstr : { *(.dynstr) } } Index: head/sys/boot/ficl/ficl.h =================================================================== --- head/sys/boot/ficl/ficl.h (revision 307323) +++ head/sys/boot/ficl/ficl.h (revision 307324) @@ -1,1157 +1,1164 @@ /******************************************************************* ** f i c l . h ** Forth Inspired Command Language ** Author: John Sadler (john_sadler@alum.mit.edu) ** Created: 19 July 1997 ** Dedicated to RHS, in loving memory ** $Id: ficl.h,v 1.18 2001/12/05 07:21:34 jsadler Exp $ *******************************************************************/ /* ** Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu) ** All rights reserved. ** ** Get the latest Ficl release at http://ficl.sourceforge.net ** ** I am interested in hearing from anyone who uses ficl. If you have ** a problem, a success story, a defect, an enhancement request, or ** if you would like to contribute to the ficl release, please ** contact me by email at the address above. ** ** L I C E N S E and D I S C L A I M E R ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions ** are met: ** 1. Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** 2. Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in the ** documentation and/or other materials provided with the distribution. ** ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** SUCH DAMAGE. */ /* $FreeBSD$ */ #if !defined (__FICL_H__) #define __FICL_H__ /* ** Ficl (Forth-inspired command language) is an ANS Forth ** interpreter written in C. Unlike traditional Forths, this ** interpreter is designed to be embedded into other systems ** as a command/macro/development prototype language. ** ** Where Forths usually view themselves as the center of the system ** and expect the rest of the system to be coded in Forth, Ficl ** acts as a component of the system. It is easy to export ** code written in C or ASM to Ficl in the style of TCL, or to invoke ** Ficl code from a compiled module. This allows you to do incremental ** development in a way that combines the best features of threaded ** languages (rapid development, quick code/test/debug cycle, ** reasonably fast) with the best features of C (everyone knows it, ** easier to support large blocks of code, efficient, type checking). ** ** Ficl provides facilities for interoperating ** with programs written in C: C functions can be exported to Ficl, ** and Ficl commands can be executed via a C calling interface. The ** interpreter is re-entrant, so it can be used in multiple instances ** in a multitasking system. Unlike Forth, Ficl's outer interpreter ** expects a text block as input, and returns to the caller after each ** text block, so the "data pump" is somewhere in external code. This ** is more like TCL than Forth, which usually expcets to be at the center ** of the system, requesting input at its convenience. Each Ficl virtual ** machine can be bound to a different I/O channel, and is independent ** of all others in in the same address space except that all virtual ** machines share a common dictionary (a sort or open symbol table that ** defines all of the elements of the language). ** ** Code is written in ANSI C for portability. ** ** Summary of Ficl features and constraints: ** - Standard: Implements the ANSI Forth CORE word set and part ** of the CORE EXT word-set, SEARCH and SEARCH EXT, TOOLS and ** TOOLS EXT, LOCAL and LOCAL ext and various extras. ** - Extensible: you can export code written in Forth, C, ** or asm in a straightforward way. Ficl provides open ** facilities for extending the language in an application ** specific way. You can even add new control structures! ** - Ficl and C can interact in two ways: Ficl can encapsulate ** C code, or C code can invoke Ficl code. ** - Thread-safe, re-entrant: The shared system dictionary ** uses a locking mechanism that you can either supply ** or stub out to provide exclusive access. Each Ficl ** virtual machine has an otherwise complete state, and ** each can be bound to a separate I/O channel (or none at all). ** - Simple encapsulation into existing systems: a basic implementation ** requires three function calls (see the example program in testmain.c). ** - ROMable: Ficl is designed to work in RAM-based and ROM code / RAM data ** environments. It does require somewhat more memory than a pure ** ROM implementation because it builds its system dictionary in ** RAM at startup time. ** - Written an ANSI C to be as simple as I can make it to understand, ** support, debug, and port. Compiles without complaint at /Az /W4 ** (require ANSI C, max warnings) under Microsoft VC++ 5. ** - Does full 32 bit math (but you need to implement ** two mixed precision math primitives (see sysdep.c)) ** - Indirect threaded interpreter is not the fastest kind of ** Forth there is (see pForth 68K for a really fast subroutine ** threaded interpreter), but it's the cleanest match to a ** pure C implementation. ** ** P O R T I N G F i c l ** ** To install Ficl on your target system, you need an ANSI C compiler ** and its runtime library. Inspect the system dependent macros and ** functions in sysdep.h and sysdep.c and edit them to suit your ** system. For example, INT16 is a short on some compilers and an ** int on others. Check the default CELL alignment controlled by ** FICL_ALIGN. If necessary, add new definitions of ficlMalloc, ficlFree, ** ficlLockDictionary, and ficlTextOut to work with your operating system. ** Finally, use testmain.c as a guide to installing the Ficl system and ** one or more virtual machines into your code. You do not need to include ** testmain.c in your build. ** ** T o D o L i s t ** ** 1. Unimplemented system dependent CORE word: key ** 2. Ficl uses the PAD in some CORE words - this violates the standard, ** but it's cleaner for a multithreaded system. I'll have to make a ** second pad for reference by the word PAD to fix this. ** ** F o r M o r e I n f o r m a t i o n ** ** Web home of ficl ** http://ficl.sourceforge.net ** Check this website for Forth literature (including the ANSI standard) ** http://www.taygeta.com/forthlit.html ** and here for software and more links ** http://www.taygeta.com/forth.html ** ** Obvious Performance enhancement opportunities ** Compile speed ** - work on interpret speed ** - turn off locals (FICL_WANT_LOCALS) ** Interpret speed ** - Change inner interpreter (and everything else) ** so that a definition is a list of pointers to functions ** and inline data rather than pointers to words. This gets ** rid of vm->runningWord and a level of indirection in the ** inner loop. I'll look at it for ficl 3.0 ** - Make the main hash table a bigger prime (HASHSIZE) ** - FORGET about twiddling the hash function - my experience is ** that that is a waste of time. ** - Eliminate the need to pass the pVM parameter on the stack ** by dedicating a register to it. Most words need access to the ** vm, but the parameter passing overhead can be reduced. One way ** requires that the host OS have a task switch callout. Create ** a global variable for the running VM and refer to it in words ** that need VM access. Alternative: use thread local storage. ** For single threaded implementations, you can just use a global. ** The first two solutions create portability problems, so I ** haven't considered doing them. Another possibility is to ** declare the pVm parameter to be "register", and hope the compiler ** pays attention. ** */ /* ** Revision History: ** ** 15 Apr 1999 (sadler) Merged FreeBSD changes for exception wordset and ** counted strings in ficlExec. ** 12 Jan 1999 (sobral) Corrected EVALUATE behavior. Now TIB has an ** "end" field, and all words respect this. ficlExec is passed a "size" ** of TIB, as well as vmPushTib. This size is used to calculate the "end" ** of the string, ie, base+size. If the size is not known, pass -1. ** ** 10 Jan 1999 (sobral) EXCEPTION word set has been added, and existing ** words has been modified to conform to EXCEPTION EXT word set. ** ** 27 Aug 1998 (sadler) testing and corrections for LOCALS, LOCALS EXT, ** SEARCH / SEARCH EXT, TOOLS / TOOLS EXT. ** Added .X to display in hex, PARSE and PARSE-WORD to supplement WORD, ** EMPTY to clear stack. ** ** 29 jun 1998 (sadler) added variable sized hash table support ** and ANS Forth optional SEARCH & SEARCH EXT word set. ** 26 May 1998 (sadler) ** FICL_PROMPT macro ** 14 April 1998 (sadler) V1.04 ** Ficlwin: Windows version, Skip Carter's Linux port ** 5 March 1998 (sadler) V1.03 ** Bug fixes -- passes John Ryan's ANS test suite "core.fr" ** ** 24 February 1998 (sadler) V1.02 ** -Fixed bugs in <# # #> ** -Changed FICL_WORD so that storage for the name characters ** can be allocated from the dictionary as needed rather than ** reserving 32 bytes in each word whether needed or not - ** this saved 50% of the dictionary storage requirement. ** -Added words in testmain for Win32 functions system,chdir,cwd, ** also added a word that loads and evaluates a file. ** ** December 1997 (sadler) ** -Added VM_RESTART exception handling in ficlExec -- this lets words ** that require additional text to succeed (like :, create, variable...) ** recover gracefully from an empty input buffer rather than emitting ** an error message. Definitions can span multiple input blocks with ** no restrictions. ** -Changed #include order so that is included in sysdep.h, ** and sysdep is included in all other files. This lets you define ** NDEBUG in sysdep.h to disable assertions if you want to. ** -Make PC specific system dependent code conditional on _M_IX86 ** defined so that ports can coexist in sysdep.h/sysdep.c */ #ifdef __cplusplus extern "C" { #endif #include "sysdep.h" #include /* UCHAR_MAX */ /* ** Forward declarations... read on. */ struct ficl_word; typedef struct ficl_word FICL_WORD; struct vm; typedef struct vm FICL_VM; struct ficl_dict; typedef struct ficl_dict FICL_DICT; struct ficl_system; typedef struct ficl_system FICL_SYSTEM; struct ficl_system_info; typedef struct ficl_system_info FICL_SYSTEM_INFO; /* ** the Good Stuff starts here... */ #define FICL_VER "3.03" #define FICL_VER_MAJOR 3 #define FICL_VER_MINOR 3 #if !defined (FICL_PROMPT) #define FICL_PROMPT "ok> " #endif /* ** ANS Forth requires false to be zero, and true to be the ones ** complement of false... that unifies logical and bitwise operations ** nicely. */ #define FICL_TRUE ((unsigned long)~(0L)) #define FICL_FALSE (0) #define FICL_BOOL(x) ((x) ? FICL_TRUE : FICL_FALSE) /* ** A CELL is the main storage type. It must be large enough ** to contain a pointer or a scalar. In order to accommodate ** 32 bit and 64 bit processors, use abstract types for int, ** unsigned, and float. */ typedef union _cell { FICL_INT i; FICL_UNS u; #if (FICL_WANT_FLOAT) FICL_FLOAT f; #endif void *p; void (*fn)(void); } CELL; /* ** LVALUEtoCELL does a little pointer trickery to cast any CELL sized ** lvalue (informal definition: an expression whose result has an ** address) to CELL. Remember that constants and casts are NOT ** themselves lvalues! */ #define LVALUEtoCELL(v) (*(CELL *)&v) /* ** PTRtoCELL is a cast through void * intended to satisfy the ** most outrageously pedantic compiler... (I won't mention ** its name) */ #define PTRtoCELL (CELL *)(void *) #define PTRtoSTRING (FICL_STRING *)(void *) /* ** Strings in FICL are stored in Pascal style - with a count ** preceding the text. We'll also NULL-terminate them so that ** they work with the usual C lib string functions. (Belt & ** suspenders? You decide.) ** STRINGINFO hides the implementation with a couple of ** macros for use in internal routines. */ typedef unsigned char FICL_COUNT; #define FICL_STRING_MAX UCHAR_MAX typedef struct _ficl_string { FICL_COUNT count; char text[1]; } FICL_STRING; typedef struct { FICL_UNS count; char *cp; } STRINGINFO; #define SI_COUNT(si) (si.count) #define SI_PTR(si) (si.cp) #define SI_SETLEN(si, len) (si.count = (FICL_UNS)(len)) #define SI_SETPTR(si, ptr) (si.cp = (char *)(ptr)) /* ** Init a STRINGINFO from a pointer to NULL-terminated string */ #define SI_PSZ(si, psz) \ {si.cp = psz; si.count = (FICL_COUNT)strlen(psz);} /* ** Init a STRINGINFO from a pointer to FICL_STRING */ #define SI_PFS(si, pfs) \ {si.cp = pfs->text; si.count = pfs->count;} /* ** Ficl uses this little structure to hold the address of ** the block of text it's working on and an index to the next ** unconsumed character in the string. Traditionally, this is ** done by a Text Input Buffer, so I've called this struct TIB. ** ** Since this structure also holds the size of the input buffer, ** and since evaluate requires that, let's put the size here. ** The size is stored as an end-pointer because that is what the ** null-terminated string aware functions find most easy to deal ** with. ** Notice, though, that nobody really uses this except evaluate, ** so it might just be moved to FICL_VM instead. (sobral) */ typedef struct { FICL_INT index; char *end; char *cp; } TIB; /* ** Stacks get heavy use in Ficl and Forth... ** Each virtual machine implements two of them: ** one holds parameters (data), and the other holds return ** addresses and control flow information for the virtual ** machine. (Note: C's automatic stack is implicitly used, ** but not modeled because it doesn't need to be...) ** Here's an abstract type for a stack */ typedef struct _ficlStack { FICL_UNS nCells; /* size of the stack */ CELL *pFrame; /* link reg for stack frame */ CELL *sp; /* stack pointer */ CELL base[1]; /* Top of stack */ } FICL_STACK; /* ** Stack methods... many map closely to required Forth words. */ FICL_STACK *stackCreate (unsigned nCells); void stackDelete (FICL_STACK *pStack); int stackDepth (FICL_STACK *pStack); void stackDrop (FICL_STACK *pStack, int n); CELL stackFetch (FICL_STACK *pStack, int n); CELL stackGetTop (FICL_STACK *pStack); void stackLink (FICL_STACK *pStack, int nCells); void stackPick (FICL_STACK *pStack, int n); CELL stackPop (FICL_STACK *pStack); void *stackPopPtr (FICL_STACK *pStack); FICL_UNS stackPopUNS (FICL_STACK *pStack); FICL_INT stackPopINT (FICL_STACK *pStack); void stackPush (FICL_STACK *pStack, CELL c); void stackPushPtr (FICL_STACK *pStack, void *ptr); void stackPushUNS (FICL_STACK *pStack, FICL_UNS u); void stackPushINT (FICL_STACK *pStack, FICL_INT i); void stackReset (FICL_STACK *pStack); void stackRoll (FICL_STACK *pStack, int n); void stackSetTop (FICL_STACK *pStack, CELL c); void stackStore (FICL_STACK *pStack, int n, CELL c); void stackUnlink (FICL_STACK *pStack); #if (FICL_WANT_FLOAT) float stackPopFloat (FICL_STACK *pStack); void stackPushFloat(FICL_STACK *pStack, FICL_FLOAT f); #endif /* ** Shortcuts (Guy Carver) */ #define PUSHPTR(p) stackPushPtr(pVM->pStack,p) #define PUSHUNS(u) stackPushUNS(pVM->pStack,u) #define PUSHINT(i) stackPushINT(pVM->pStack,i) #define PUSHFLOAT(f) stackPushFloat(pVM->fStack,f) #define PUSH(c) stackPush(pVM->pStack,c) #define POPPTR() stackPopPtr(pVM->pStack) #define POPUNS() stackPopUNS(pVM->pStack) #define POPINT() stackPopINT(pVM->pStack) #define POPFLOAT() stackPopFloat(pVM->fStack) #define POP() stackPop(pVM->pStack) #define GETTOP() stackGetTop(pVM->pStack) #define SETTOP(c) stackSetTop(pVM->pStack,LVALUEtoCELL(c)) #define GETTOPF() stackGetTop(pVM->fStack) #define SETTOPF(c) stackSetTop(pVM->fStack,LVALUEtoCELL(c)) #define STORE(n,c) stackStore(pVM->pStack,n,LVALUEtoCELL(c)) #define DEPTH() stackDepth(pVM->pStack) #define DROP(n) stackDrop(pVM->pStack,n) #define DROPF(n) stackDrop(pVM->fStack,n) #define FETCH(n) stackFetch(pVM->pStack,n) #define PICK(n) stackPick(pVM->pStack,n) #define PICKF(n) stackPick(pVM->fStack,n) #define ROLL(n) stackRoll(pVM->pStack,n) #define ROLLF(n) stackRoll(pVM->fStack,n) /* ** The virtual machine (VM) contains the state for one interpreter. ** Defined operations include: ** Create & initialize ** Delete ** Execute a block of text ** Parse a word out of the input stream ** Call return, and branch ** Text output ** Throw an exception */ typedef FICL_WORD ** IPTYPE; /* the VM's instruction pointer */ /* ** Each VM has a placeholder for an output function - ** this makes it possible to have each VM do I/O ** through a different device. If you specify no ** OUTFUNC, it defaults to ficlTextOut. */ typedef void (*OUTFUNC)(FICL_VM *pVM, char *text, int fNewline); /* ** Each VM operates in one of two non-error states: interpreting ** or compiling. When interpreting, words are simply executed. ** When compiling, most words in the input stream have their ** addresses inserted into the word under construction. Some words ** (known as IMMEDIATE) are executed in the compile state, too. */ /* values of STATE */ #define INTERPRET 0 #define COMPILE 1 /* ** The pad is a small scratch area for text manipulation. ANS Forth ** requires it to hold at least 84 characters. */ #if !defined nPAD #define nPAD 256 #endif /* ** ANS Forth requires that a word's name contain {1..31} characters. */ #if !defined nFICLNAME #define nFICLNAME 31 #endif /* ** OK - now we can really define the VM... */ struct vm { FICL_SYSTEM *pSys; /* Which system this VM belongs to */ FICL_VM *link; /* Ficl keeps a VM list for simple teardown */ jmp_buf *pState; /* crude exception mechanism... */ OUTFUNC textOut; /* Output callback - see sysdep.c */ void * pExtend; /* vm extension pointer for app use - initialized from FICL_SYSTEM */ short fRestart; /* Set TRUE to restart runningWord */ IPTYPE ip; /* instruction pointer */ FICL_WORD *runningWord;/* address of currently running word (often just *(ip-1) ) */ FICL_UNS state; /* compiling or interpreting */ FICL_UNS base; /* number conversion base */ FICL_STACK *pStack; /* param stack */ FICL_STACK *rStack; /* return stack */ #if FICL_WANT_FLOAT FICL_STACK *fStack; /* float stack (optional) */ #endif CELL sourceID; /* -1 if EVALUATE, 0 if normal input */ TIB tib; /* address of incoming text string */ #if FICL_WANT_USER CELL user[FICL_USER_CELLS]; #endif char pad[nPAD]; /* the scratch area (see above) */ }; /* ** A FICL_CODE points to a function that gets called to help execute ** a word in the dictionary. It always gets passed a pointer to the ** running virtual machine, and from there it can get the address ** of the parameter area of the word it's supposed to operate on. ** For precompiled words, the code is all there is. For user defined ** words, the code assumes that the word's parameter area is a list ** of pointers to the code fields of other words to execute, and ** may also contain inline data. The first parameter is always ** a pointer to a code field. */ typedef void (*FICL_CODE)(FICL_VM *pVm); #if 0 #define VM_ASSERT(pVM) assert((*(pVM->ip - 1)) == pVM->runningWord) #else #define VM_ASSERT(pVM) #endif /* ** Ficl models memory as a contiguous space divided into ** words in a linked list called the dictionary. ** A FICL_WORD starts each entry in the list. ** Version 1.02: space for the name characters is allotted from ** the dictionary ahead of the word struct, rather than using ** a fixed size array for each name. */ struct ficl_word { struct ficl_word *link; /* Previous word in the dictionary */ UNS16 hash; UNS8 flags; /* Immediate, Smudge, Compile-only */ FICL_COUNT nName; /* Number of chars in word name */ char *name; /* First nFICLNAME chars of word name */ FICL_CODE code; /* Native code to execute the word */ CELL param[1]; /* First data cell of the word */ }; /* ** Worst-case size of a word header: nFICLNAME chars in name */ #define CELLS_PER_WORD \ ( (sizeof (FICL_WORD) + nFICLNAME + sizeof (CELL)) \ / (sizeof (CELL)) ) int wordIsImmediate(FICL_WORD *pFW); int wordIsCompileOnly(FICL_WORD *pFW); /* flag values for word header */ #define FW_IMMEDIATE 1 /* execute me even if compiling */ #define FW_COMPILE 2 /* error if executed when not compiling */ #define FW_SMUDGE 4 /* definition in progress - hide me */ #define FW_ISOBJECT 8 /* word is an object or object member variable */ #define FW_COMPIMMED (FW_IMMEDIATE | FW_COMPILE) #define FW_DEFAULT 0 /* ** Exit codes for vmThrow */ #define VM_INNEREXIT -256 /* tell ficlExecXT to exit inner loop */ #define VM_OUTOFTEXT -257 /* hungry - normal exit */ #define VM_RESTART -258 /* word needs more text to succeed - re-run it */ #define VM_USEREXIT -259 /* user wants to quit */ #define VM_ERREXIT -260 /* interp found an error */ #define VM_BREAK -261 /* debugger breakpoint */ #define VM_ABORT -1 /* like errexit -- abort */ #define VM_ABORTQ -2 /* like errexit -- abort" */ #define VM_QUIT -56 /* like errexit, but leave pStack & base alone */ void vmBranchRelative(FICL_VM *pVM, int offset); FICL_VM * vmCreate (FICL_VM *pVM, unsigned nPStack, unsigned nRStack); void vmDelete (FICL_VM *pVM); void vmExecute (FICL_VM *pVM, FICL_WORD *pWord); FICL_DICT *vmGetDict (FICL_VM *pVM); char * vmGetString (FICL_VM *pVM, FICL_STRING *spDest, char delimiter); STRINGINFO vmGetWord (FICL_VM *pVM); STRINGINFO vmGetWord0 (FICL_VM *pVM); int vmGetWordToPad (FICL_VM *pVM); STRINGINFO vmParseString (FICL_VM *pVM, char delimiter); STRINGINFO vmParseStringEx(FICL_VM *pVM, char delimiter, char fSkipLeading); CELL vmPop (FICL_VM *pVM); void vmPush (FICL_VM *pVM, CELL c); void vmPopIP (FICL_VM *pVM); void vmPushIP (FICL_VM *pVM, IPTYPE newIP); void vmQuit (FICL_VM *pVM); void vmReset (FICL_VM *pVM); void vmSetTextOut (FICL_VM *pVM, OUTFUNC textOut); void vmTextOut (FICL_VM *pVM, char *text, int fNewline); void vmTextOut (FICL_VM *pVM, char *text, int fNewline); void vmThrow (FICL_VM *pVM, int except); void vmThrowErr (FICL_VM *pVM, char *fmt, ...); #define vmGetRunningWord(pVM) ((pVM)->runningWord) /* ** The inner interpreter - coded as a macro (see note for ** INLINE_INNER_LOOP in sysdep.h for complaints about VC++ 5 */ #define M_VM_STEP(pVM) \ FICL_WORD *tempFW = *(pVM)->ip++; \ (pVM)->runningWord = tempFW; \ tempFW->code(pVM); #define M_INNER_LOOP(pVM) \ for (;;) { M_VM_STEP(pVM) } #if INLINE_INNER_LOOP != 0 #define vmInnerLoop(pVM) M_INNER_LOOP(pVM) #else void vmInnerLoop(FICL_VM *pVM); #endif /* ** vmCheckStack needs a vm pointer because it might have to say ** something if it finds a problem. Parms popCells and pushCells ** correspond to the number of parameters on the left and right of ** a word's stack effect comment. */ void vmCheckStack(FICL_VM *pVM, int popCells, int pushCells); #if FICL_WANT_FLOAT void vmCheckFStack(FICL_VM *pVM, int popCells, int pushCells); #endif /* ** TIB access routines... ** ANS forth seems to require the input buffer to be represented ** as a pointer to the start of the buffer, and an index to the ** next character to read. ** PushTib points the VM to a new input string and optionally ** returns a copy of the current state ** PopTib restores the TIB state given a saved TIB from PushTib ** GetInBuf returns a pointer to the next unused char of the TIB */ void vmPushTib (FICL_VM *pVM, char *text, FICL_INT nChars, TIB *pSaveTib); void vmPopTib (FICL_VM *pVM, TIB *pTib); #define vmGetInBuf(pVM) ((pVM)->tib.cp + (pVM)->tib.index) #define vmGetInBufLen(pVM) ((pVM)->tib.end - (pVM)->tib.cp) #define vmGetInBufEnd(pVM) ((pVM)->tib.end) #define vmGetTibIndex(pVM) (pVM)->tib.index #define vmSetTibIndex(pVM, i) (pVM)->tib.index = i #define vmUpdateTib(pVM, str) (pVM)->tib.index = (str) - (pVM)->tib.cp /* ** Generally useful string manipulators omitted by ANSI C... ** ltoa complements strtol */ #if defined(_WIN32) && !FICL_MAIN /* #SHEESH ** Why do Microsoft Meatballs insist on contaminating ** my namespace with their string functions??? */ #pragma warning(disable: 4273) #endif int isPowerOfTwo(FICL_UNS u); char *ltoa( FICL_INT value, char *string, int radix ); char *ultoa(FICL_UNS value, char *string, int radix ); char digit_to_char(int value); char *strrev( char *string ); char *skipSpace(char *cp, char *end); char *caseFold(char *cp); int strincmp(char *cp1, char *cp2, FICL_UNS count); #if defined(_WIN32) && !FICL_MAIN #pragma warning(default: 4273) #endif /* ** Ficl hash table - variable size. ** assert(size > 0) ** If size is 1, the table degenerates into a linked list. ** A WORDLIST (see the search order word set in DPANS) is ** just a pointer to a FICL_HASH in this implementation. */ #if !defined HASHSIZE /* Default size of hash table. For most uniform */ #define HASHSIZE 241 /* performance, use a prime number! */ #endif typedef struct ficl_hash { struct ficl_hash *link; /* link to parent class wordlist for OO */ char *name; /* optional pointer to \0 terminated wordlist name */ unsigned size; /* number of buckets in the hash */ FICL_WORD *table[1]; } FICL_HASH; void hashForget (FICL_HASH *pHash, void *where); UNS16 hashHashCode (STRINGINFO si); void hashInsertWord(FICL_HASH *pHash, FICL_WORD *pFW); FICL_WORD *hashLookup (FICL_HASH *pHash, STRINGINFO si, UNS16 hashCode); void hashReset (FICL_HASH *pHash); /* ** A Dictionary is a linked list of FICL_WORDs. It is also Ficl's ** memory model. Description of fields: ** ** here -- points to the next free byte in the dictionary. This ** pointer is forced to be CELL-aligned before a definition is added. ** Do not assume any specific alignment otherwise - Use dictAlign(). ** ** smudge -- pointer to word currently being defined (or last defined word) ** If the definition completes successfully, the word will be ** linked into the hash table. If unsuccessful, dictUnsmudge ** uses this pointer to restore the previous state of the dictionary. ** Smudge prevents unintentional recursion as a side-effect: the ** dictionary search algo examines only completed definitions, so a ** word cannot invoke itself by name. See the ficl word "recurse". ** NOTE: smudge always points to the last word defined. IMMEDIATE ** makes use of this fact. Smudge is initially NULL. ** ** pForthWords -- pointer to the default wordlist (FICL_HASH). ** This is the initial compilation list, and contains all ** ficl's precompiled words. ** ** pCompile -- compilation wordlist - initially equal to pForthWords ** pSearch -- array of pointers to wordlists. Managed as a stack. ** Highest index is the first list in the search order. ** nLists -- number of lists in pSearch. nLists-1 is the highest ** filled slot in pSearch, and points to the first wordlist ** in the search order ** size -- number of cells in the dictionary (total) ** dict -- start of data area. Must be at the end of the struct. */ struct ficl_dict { CELL *here; FICL_WORD *smudge; FICL_HASH *pForthWords; FICL_HASH *pCompile; FICL_HASH *pSearch[FICL_DEFAULT_VOCS]; int nLists; unsigned size; /* Number of cells in dict (total)*/ CELL *dict; /* Base of dictionary memory */ }; void *alignPtr(void *ptr); void dictAbortDefinition(FICL_DICT *pDict); void dictAlign (FICL_DICT *pDict); int dictAllot (FICL_DICT *pDict, int n); int dictAllotCells (FICL_DICT *pDict, int nCells); void dictAppendCell (FICL_DICT *pDict, CELL c); void dictAppendChar (FICL_DICT *pDict, char c); FICL_WORD *dictAppendWord (FICL_DICT *pDict, char *name, FICL_CODE pCode, UNS8 flags); FICL_WORD *dictAppendWord2(FICL_DICT *pDict, STRINGINFO si, FICL_CODE pCode, UNS8 flags); void dictAppendUNS (FICL_DICT *pDict, FICL_UNS u); int dictCellsAvail (FICL_DICT *pDict); int dictCellsUsed (FICL_DICT *pDict); void dictCheck (FICL_DICT *pDict, FICL_VM *pVM, int n); void dictCheckThreshold(FICL_DICT* dp); FICL_DICT *dictCreate(unsigned nCELLS); FICL_DICT *dictCreateHashed(unsigned nCells, unsigned nHash); FICL_HASH *dictCreateWordlist(FICL_DICT *dp, int nBuckets); void dictDelete (FICL_DICT *pDict); void dictEmpty (FICL_DICT *pDict, unsigned nHash); #if FICL_WANT_FLOAT void dictHashSummary(FICL_VM *pVM); #endif int dictIncludes (FICL_DICT *pDict, void *p); FICL_WORD *dictLookup (FICL_DICT *pDict, STRINGINFO si); #if FICL_WANT_LOCALS FICL_WORD *ficlLookupLoc (FICL_SYSTEM *pSys, STRINGINFO si); #endif void dictResetSearchOrder(FICL_DICT *pDict); void dictSetFlags (FICL_DICT *pDict, UNS8 set, UNS8 clr); void dictSetImmediate(FICL_DICT *pDict); void dictUnsmudge (FICL_DICT *pDict); CELL *dictWhere (FICL_DICT *pDict); /* ** P A R S E S T E P ** (New for 2.05) ** See words.c: interpWord ** By default, ficl goes through two attempts to parse each token from its input ** stream: it first attempts to match it with a word in the dictionary, and ** if that fails, it attempts to convert it into a number. This mechanism is now ** extensible by additional steps. This allows extensions like floating point and ** double number support to be factored cleanly. ** ** Each parse step is a function that receives the next input token as a STRINGINFO. ** If the parse step matches the token, it must apply semantics to the token appropriate ** to the present value of VM.state (compiling or interpreting), and return FICL_TRUE. ** Otherwise it returns FICL_FALSE. See words.c: isNumber for an example ** ** Note: for the sake of efficiency, it's a good idea both to limit the number ** of parse steps and to code each parse step so that it rejects tokens that ** do not match as quickly as possible. */ typedef int (*FICL_PARSE_STEP)(FICL_VM *pVM, STRINGINFO si); /* ** Appends a parse step function to the end of the parse list (see ** FICL_PARSE_STEP notes in ficl.h for details). Returns 0 if successful, ** nonzero if there's no more room in the list. Each parse step is a word in ** the dictionary. Precompiled parse steps can use (PARSE-STEP) as their ** CFA - see parenParseStep in words.c. */ int ficlAddParseStep(FICL_SYSTEM *pSys, FICL_WORD *pFW); /* ficl.c */ void ficlAddPrecompiledParseStep(FICL_SYSTEM *pSys, char *name, FICL_PARSE_STEP pStep); void ficlListParseSteps(FICL_VM *pVM); /* ** FICL_BREAKPOINT record. ** origXT - if NULL, this breakpoint is unused. Otherwise it stores the xt ** that the breakpoint overwrote. This is restored to the dictionary when the ** BP executes or gets cleared ** address - the location of the breakpoint (address of the instruction that ** has been replaced with the breakpoint trap ** origXT - The original contents of the location with the breakpoint ** Note: address is NULL when this breakpoint is empty */ typedef struct FICL_BREAKPOINT { void *address; FICL_WORD *origXT; } FICL_BREAKPOINT; /* ** F I C L _ S Y S T E M ** The top level data structure of the system - ficl_system ties a list of ** virtual machines with their corresponding dictionaries. Ficl 3.0 will ** support multiple Ficl systems, allowing multiple concurrent sessions ** to separate dictionaries with some constraints. ** The present model allows multiple sessions to one dictionary provided ** you implement ficlLockDictionary() as specified in sysdep.h ** Note: the pExtend pointer is there to provide context for applications. It is copied ** to each VM's pExtend field as that VM is created. */ struct ficl_system { FICL_SYSTEM *link; void *pExtend; /* Initializes VM's pExtend pointer (for application use) */ FICL_VM *vmList; FICL_DICT *dp; FICL_DICT *envp; #ifdef FICL_WANT_LOCALS FICL_DICT *localp; #endif FICL_WORD *pInterp[3]; FICL_WORD *parseList[FICL_MAX_PARSE_STEPS]; OUTFUNC textOut; FICL_WORD *pBranchParen; FICL_WORD *pDoParen; FICL_WORD *pDoesParen; FICL_WORD *pExitInner; FICL_WORD *pExitParen; FICL_WORD *pBranch0; FICL_WORD *pInterpret; FICL_WORD *pLitParen; FICL_WORD *pTwoLitParen; FICL_WORD *pLoopParen; FICL_WORD *pPLoopParen; FICL_WORD *pQDoParen; FICL_WORD *pSemiParen; FICL_WORD *pOfParen; FICL_WORD *pStore; FICL_WORD *pDrop; FICL_WORD *pCStringLit; FICL_WORD *pStringLit; #if FICL_WANT_LOCALS FICL_WORD *pGetLocalParen; FICL_WORD *pGet2LocalParen; FICL_WORD *pGetLocal0; FICL_WORD *pGetLocal1; FICL_WORD *pToLocalParen; FICL_WORD *pTo2LocalParen; FICL_WORD *pToLocal0; FICL_WORD *pToLocal1; FICL_WORD *pLinkParen; FICL_WORD *pUnLinkParen; FICL_INT nLocals; CELL *pMarkLocals; #endif FICL_BREAKPOINT bpStep; }; struct ficl_system_info { int size; /* structure size tag for versioning */ int nDictCells; /* Size of system's Dictionary */ OUTFUNC textOut; /* default textOut function */ void *pExtend; /* Initializes VM's pExtend pointer - for application use */ int nEnvCells; /* Size of Environment dictionary */ }; #define ficlInitInfo(x) { memset((x), 0, sizeof(FICL_SYSTEM_INFO)); \ (x)->size = sizeof(FICL_SYSTEM_INFO); } /* ** External interface to FICL... */ /* ** f i c l I n i t S y s t e m ** Binds a global dictionary to the interpreter system and initializes ** the dict to contain the ANSI CORE wordset. ** You can specify the address and size of the allocated area. ** Using ficlInitSystemEx you can also specify the text output function. ** After that, ficl manages it. ** First step is to set up the static pointers to the area. ** Then write the "precompiled" portion of the dictionary in. ** The dictionary needs to be at least large enough to hold the ** precompiled part. Try 1K cells minimum. Use "words" to find ** out how much of the dictionary is used at any time. */ FICL_SYSTEM *ficlInitSystemEx(FICL_SYSTEM_INFO *fsi); /* Deprecated call */ FICL_SYSTEM *ficlInitSystem(int nDictCells); /* ** f i c l T e r m S y s t e m ** Deletes the system dictionary and all virtual machines that ** were created with ficlNewVM (see below). Call this function to ** reclaim all memory used by the dictionary and VMs. */ void ficlTermSystem(FICL_SYSTEM *pSys); /* ** f i c l E v a l u a t e ** Evaluates a block of input text in the context of the ** specified interpreter. Also sets SOURCE-ID properly. ** ** PLEASE USE THIS FUNCTION when throwing a hard-coded ** string to the FICL interpreter. */ int ficlEvaluate(FICL_VM *pVM, char *pText); /* ** f i c l E x e c ** Evaluates a block of input text in the context of the ** specified interpreter. Emits any requested output to the ** interpreter's output function. If the input string is NULL ** terminated, you can pass -1 as nChars rather than count it. ** Execution returns when the text block has been executed, ** or an error occurs. ** Returns one of the VM_XXXX codes defined in ficl.h: ** VM_OUTOFTEXT is the normal exit condition ** VM_ERREXIT means that the interp encountered a syntax error ** and the vm has been reset to recover (some or all ** of the text block got ignored ** VM_USEREXIT means that the user executed the "bye" command ** to shut down the interpreter. This would be a good ** time to delete the vm, etc -- or you can ignore this ** signal. ** VM_ABORT and VM_ABORTQ are generated by 'abort' and 'abort"' ** commands. ** Preconditions: successful execution of ficlInitSystem, ** Successful creation and init of the VM by ficlNewVM (or equiv) ** ** If you call ficlExec() or one of its brothers, you MUST ** ensure pVM->sourceID was set to a sensible value. ** ficlExec() explicitly DOES NOT manage SOURCE-ID for you. */ int ficlExec (FICL_VM *pVM, char *pText); int ficlExecC(FICL_VM *pVM, char *pText, FICL_INT nChars); int ficlExecXT(FICL_VM *pVM, FICL_WORD *pWord); /* ** ficlExecFD(FICL_VM *pVM, int fd); * Evaluates text from file passed in via fd. * Execution returns when all of file has been executed or an * error occurs. */ int ficlExecFD(FICL_VM *pVM, int fd); /* ** Create a new VM from the heap, and link it into the system VM list. ** Initializes the VM and binds default sized stacks to it. Returns the ** address of the VM, or NULL if an error occurs. ** Precondition: successful execution of ficlInitSystem */ FICL_VM *ficlNewVM(FICL_SYSTEM *pSys); /* ** Force deletion of a VM. You do not need to do this ** unless you're creating and discarding a lot of VMs. ** For systems that use a constant pool of VMs for the life ** of the system, ficltermSystem takes care of VM cleanup ** automatically. */ void ficlFreeVM(FICL_VM *pVM); /* ** Set the stack sizes (return and parameter) to be used for all ** subsequently created VMs. Returns actual stack size to be used. */ int ficlSetStackSize(int nStackCells); /* ** Returns the address of the most recently defined word in the system ** dictionary with the given name, or NULL if no match. ** Precondition: successful execution of ficlInitSystem */ FICL_WORD *ficlLookup(FICL_SYSTEM *pSys, char *name); /* ** f i c l G e t D i c t ** Utility function - returns the address of the system dictionary. ** Precondition: successful execution of ficlInitSystem */ FICL_DICT *ficlGetDict(FICL_SYSTEM *pSys); FICL_DICT *ficlGetEnv (FICL_SYSTEM *pSys); void ficlSetEnv (FICL_SYSTEM *pSys, char *name, FICL_UNS value); void ficlSetEnvD(FICL_SYSTEM *pSys, char *name, FICL_UNS hi, FICL_UNS lo); #if FICL_WANT_LOCALS FICL_DICT *ficlGetLoc (FICL_SYSTEM *pSys); #endif /* ** f i c l B u i l d ** Builds a word into the system default dictionary in a thread-safe way. ** Preconditions: system must be initialized, and there must ** be enough space for the new word's header! Operation is ** controlled by ficlLockDictionary, so any initialization ** required by your version of the function (if you "overrode" ** it) must be complete at this point. ** Parameters: ** name -- the name of the word to be built ** code -- code to execute when the word is invoked - must take a single param ** pointer to a FICL_VM ** flags -- 0 or more of FW_IMMEDIATE, FW_COMPILE, use bitwise OR! ** Most words can use FW_DEFAULT. ** nAllot - number of extra cells to allocate in the parameter area (usually zero) */ int ficlBuild(FICL_SYSTEM *pSys, char *name, FICL_CODE code, char flags); /* ** f i c l C o m p i l e C o r e ** Builds the ANS CORE wordset into the dictionary - called by ** ficlInitSystem - no need to waste dict space by doing it again. */ void ficlCompileCore(FICL_SYSTEM *pSys); void ficlCompilePrefix(FICL_SYSTEM *pSys); void ficlCompileSearch(FICL_SYSTEM *pSys); void ficlCompileSoftCore(FICL_SYSTEM *pSys); void ficlCompileTools(FICL_SYSTEM *pSys); void ficlCompileFile(FICL_SYSTEM *pSys); #if FICL_WANT_FLOAT void ficlCompileFloat(FICL_SYSTEM *pSys); int ficlParseFloatNumber( FICL_VM *pVM, STRINGINFO si ); /* float.c */ #endif #if FICL_PLATFORM_EXTEND void ficlCompilePlatform(FICL_SYSTEM *pSys); #endif int ficlParsePrefix(FICL_VM *pVM, STRINGINFO si); /* ** from words.c... */ void constantParen(FICL_VM *pVM); void twoConstParen(FICL_VM *pVM); int ficlParseNumber(FICL_VM *pVM, STRINGINFO si); void ficlTick(FICL_VM *pVM); void parseStepParen(FICL_VM *pVM); /* ** From tools.c */ int isAFiclWord(FICL_DICT *pd, FICL_WORD *pFW); /* ** The following supports SEE and the debugger. */ typedef enum { BRANCH, COLON, CONSTANT, CREATE, DO, DOES, IF, LITERAL, LOOP, OF, PLOOP, PRIMITIVE, QDO, STRINGLIT, CSTRINGLIT, #if FICL_WANT_USER USER, #endif VARIABLE, } WORDKIND; WORDKIND ficlWordClassify(FICL_WORD *pFW); /* ** Dictionary on-demand resizing */ extern CELL dictThreshold; extern CELL dictIncrease; /* ** Various FreeBSD goodies */ #if defined(__i386__) && !defined(TESTMAIN) extern void ficlOutb(FICL_VM *pVM); extern void ficlInb(FICL_VM *pVM); #endif extern void ficlSetenv(FICL_VM *pVM); extern void ficlSetenvq(FICL_VM *pVM); extern void ficlGetenv(FICL_VM *pVM); extern void ficlUnsetenv(FICL_VM *pVM); extern void ficlCopyin(FICL_VM *pVM); extern void ficlCopyout(FICL_VM *pVM); extern void ficlFindfile(FICL_VM *pVM); extern void ficlCcall(FICL_VM *pVM); #if !defined(TESTMAIN) extern void ficlPnpdevices(FICL_VM *pVM); extern void ficlPnphandlers(FICL_VM *pVM); #endif /* ** Used with File-Access wordset. */ #define FICL_FAM_READ 1 #define FICL_FAM_WRITE 2 #define FICL_FAM_APPEND 4 #define FICL_FAM_BINARY 8 #define FICL_FAM_OPEN_MODE(fam) ((fam) & (FICL_FAM_READ | FICL_FAM_WRITE | FICL_FAM_APPEND)) #if (FICL_WANT_FILE) typedef struct ficlFILE { FILE *f; char filename[256]; } ficlFILE; #endif +#include + +typedef void ficlCompileFcn(FICL_SYSTEM *); +#define FICL_COMPILE_SET(func) \ + DATA_SET(Xficl_compile_set, func) +SET_DECLARE(Xficl_compile_set, ficlCompileFcn); + #ifdef __cplusplus } #endif #endif /* __FICL_H__ */ Index: head/sys/boot/ficl/i386/sysdep.c =================================================================== --- head/sys/boot/ficl/i386/sysdep.c (revision 307323) +++ head/sys/boot/ficl/i386/sysdep.c (revision 307324) @@ -1,135 +1,149 @@ /******************************************************************* ** s y s d e p . c ** Forth Inspired Command Language ** Author: John Sadler (john_sadler@alum.mit.edu) ** Created: 16 Oct 1997 ** Implementations of FICL external interface functions... ** *******************************************************************/ /* $FreeBSD$ */ #ifdef TESTMAIN #include #include #else #include #ifdef __i386__ #include #endif #endif #include "ficl.h" /* ******************* FreeBSD P O R T B E G I N S H E R E ******************** Michael Smith */ #if PORTABLE_LONGMULDIV == 0 DPUNS ficlLongMul(FICL_UNS x, FICL_UNS y) { DPUNS q; u_int64_t qx; qx = (u_int64_t)x * (u_int64_t) y; q.hi = (u_int32_t)( qx >> 32 ); q.lo = (u_int32_t)( qx & 0xFFFFFFFFL); return q; } UNSQR ficlLongDiv(DPUNS q, FICL_UNS y) { UNSQR result; u_int64_t qx, qh; qh = q.hi; qx = (qh << 32) | q.lo; result.quot = qx / y; result.rem = qx % y; return result; } #endif void ficlTextOut(FICL_VM *pVM, char *msg, int fNewline) { IGNORE(pVM); while(*msg != 0) putchar((unsigned char)*(msg++)); if (fNewline) putchar('\n'); return; } void *ficlMalloc (size_t size) { return malloc(size); } void *ficlRealloc (void *p, size_t size) { return realloc(p, size); } void ficlFree (void *p) { free(p); } #ifndef TESTMAIN -#ifdef __i386__ /* * outb ( port# c -- ) * Store a byte to I/O port number port# */ void ficlOutb(FICL_VM *pVM) { u_char c; u_int32_t port; port=stackPopUNS(pVM->pStack); c=(u_char)stackPopINT(pVM->pStack); outb(port,c); } /* * inb ( port# -- c ) * Fetch a byte from I/O port number port# */ void ficlInb(FICL_VM *pVM) { u_char c; u_int32_t port; port=stackPopUNS(pVM->pStack); c=inb(port); stackPushINT(pVM->pStack,c); } -#endif + +/* + * Glue function to add the appropriate forth words to access x86 special cpu + * functionality. + */ +static void ficlCompileCpufunc(FICL_SYSTEM *pSys) +{ + FICL_DICT *dp = pSys->dp; + assert (dp); + + dictAppendWord(dp, "outb", ficlOutb, FW_DEFAULT); + dictAppendWord(dp, "inb", ficlInb, FW_DEFAULT); +} + +FICL_COMPILE_SET(ficlCompileCpufunc); + #endif /* ** Stub function for dictionary access control - does nothing ** by default, user can redefine to guarantee exclusive dict ** access to a single thread for updates. All dict update code ** is guaranteed to be bracketed as follows: ** ficlLockDictionary(TRUE); ** ** ficlLockDictionary(FALSE); ** ** Returns zero if successful, nonzero if unable to acquire lock ** befor timeout (optional - could also block forever) */ #if FICL_MULTITHREAD int ficlLockDictionary(short fLock) { IGNORE(fLock); return 0; } #endif /* FICL_MULTITHREAD */ Index: head/sys/boot/ficl/loader.c =================================================================== --- head/sys/boot/ficl/loader.c (revision 307323) +++ head/sys/boot/ficl/loader.c (revision 307324) @@ -1,1027 +1,842 @@ /*- * Copyright (c) 2000 Daniel Capo Sobral * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ /******************************************************************* ** l o a d e r . c ** Additional FICL words designed for FreeBSD's loader ** *******************************************************************/ #ifdef TESTMAIN #include #include #include #include #include #include #include #else #include #endif #include "bootstrap.h" #include #include #include "ficl.h" /* FreeBSD's loader interaction words and extras * * setenv ( value n name n' -- ) * setenv? ( value n name n' flag -- ) * getenv ( addr n -- addr' n' | -1 ) * unsetenv ( addr n -- ) * copyin ( addr addr' len -- ) * copyout ( addr addr' len -- ) * findfile ( name len type len' -- addr ) * pnpdevices ( -- addr ) * pnphandlers ( -- addr ) * ccall ( [[...[p10] p9] ... p1] n addr -- result ) * uuid-from-string ( addr n -- addr' ) * uuid-to-string ( addr' -- addr n ) * .# ( value -- ) */ void ficlSetenv(FICL_VM *pVM) { #ifndef TESTMAIN char *name, *value; #endif char *namep, *valuep; int names, values; #if FICL_ROBUST > 1 vmCheckStack(pVM, 4, 0); #endif names = stackPopINT(pVM->pStack); namep = (char*) stackPopPtr(pVM->pStack); values = stackPopINT(pVM->pStack); valuep = (char*) stackPopPtr(pVM->pStack); #ifndef TESTMAIN name = (char*) ficlMalloc(names+1); if (!name) vmThrowErr(pVM, "Error: out of memory"); strncpy(name, namep, names); name[names] = '\0'; value = (char*) ficlMalloc(values+1); if (!value) vmThrowErr(pVM, "Error: out of memory"); strncpy(value, valuep, values); value[values] = '\0'; setenv(name, value, 1); ficlFree(name); ficlFree(value); #endif return; } void ficlSetenvq(FICL_VM *pVM) { #ifndef TESTMAIN char *name, *value; #endif char *namep, *valuep; int names, values, overwrite; #if FICL_ROBUST > 1 vmCheckStack(pVM, 5, 0); #endif overwrite = stackPopINT(pVM->pStack); names = stackPopINT(pVM->pStack); namep = (char*) stackPopPtr(pVM->pStack); values = stackPopINT(pVM->pStack); valuep = (char*) stackPopPtr(pVM->pStack); #ifndef TESTMAIN name = (char*) ficlMalloc(names+1); if (!name) vmThrowErr(pVM, "Error: out of memory"); strncpy(name, namep, names); name[names] = '\0'; value = (char*) ficlMalloc(values+1); if (!value) vmThrowErr(pVM, "Error: out of memory"); strncpy(value, valuep, values); value[values] = '\0'; setenv(name, value, overwrite); ficlFree(name); ficlFree(value); #endif return; } void ficlGetenv(FICL_VM *pVM) { #ifndef TESTMAIN char *name, *value; #endif char *namep; int names; #if FICL_ROBUST > 1 vmCheckStack(pVM, 2, 2); #endif names = stackPopINT(pVM->pStack); namep = (char*) stackPopPtr(pVM->pStack); #ifndef TESTMAIN name = (char*) ficlMalloc(names+1); if (!name) vmThrowErr(pVM, "Error: out of memory"); strncpy(name, namep, names); name[names] = '\0'; value = getenv(name); ficlFree(name); if(value != NULL) { stackPushPtr(pVM->pStack, value); stackPushINT(pVM->pStack, strlen(value)); } else #endif stackPushINT(pVM->pStack, -1); return; } void ficlUnsetenv(FICL_VM *pVM) { #ifndef TESTMAIN char *name; #endif char *namep; int names; #if FICL_ROBUST > 1 vmCheckStack(pVM, 2, 0); #endif names = stackPopINT(pVM->pStack); namep = (char*) stackPopPtr(pVM->pStack); #ifndef TESTMAIN name = (char*) ficlMalloc(names+1); if (!name) vmThrowErr(pVM, "Error: out of memory"); strncpy(name, namep, names); name[names] = '\0'; unsetenv(name); ficlFree(name); #endif return; } void ficlCopyin(FICL_VM *pVM) { void* src; vm_offset_t dest; size_t len; #if FICL_ROBUST > 1 vmCheckStack(pVM, 3, 0); #endif len = stackPopINT(pVM->pStack); dest = stackPopINT(pVM->pStack); src = stackPopPtr(pVM->pStack); #ifndef TESTMAIN archsw.arch_copyin(src, dest, len); #endif return; } void ficlCopyout(FICL_VM *pVM) { void* dest; vm_offset_t src; size_t len; #if FICL_ROBUST > 1 vmCheckStack(pVM, 3, 0); #endif len = stackPopINT(pVM->pStack); dest = stackPopPtr(pVM->pStack); src = stackPopINT(pVM->pStack); #ifndef TESTMAIN archsw.arch_copyout(src, dest, len); #endif return; } void ficlFindfile(FICL_VM *pVM) { #ifndef TESTMAIN char *name, *type; #endif char *namep, *typep; struct preloaded_file* fp; int names, types; #if FICL_ROBUST > 1 vmCheckStack(pVM, 4, 1); #endif types = stackPopINT(pVM->pStack); typep = (char*) stackPopPtr(pVM->pStack); names = stackPopINT(pVM->pStack); namep = (char*) stackPopPtr(pVM->pStack); #ifndef TESTMAIN name = (char*) ficlMalloc(names+1); if (!name) vmThrowErr(pVM, "Error: out of memory"); strncpy(name, namep, names); name[names] = '\0'; type = (char*) ficlMalloc(types+1); if (!type) vmThrowErr(pVM, "Error: out of memory"); strncpy(type, typep, types); type[types] = '\0'; fp = file_findfile(name, type); #else fp = NULL; #endif stackPushPtr(pVM->pStack, fp); return; } -#ifndef TESTMAIN -#ifdef HAVE_PNP - void -ficlPnpdevices(FICL_VM *pVM) -{ - static int pnp_devices_initted = 0; -#if FICL_ROBUST > 1 - vmCheckStack(pVM, 0, 1); -#endif - - if(!pnp_devices_initted) { - STAILQ_INIT(&pnp_devices); - pnp_devices_initted = 1; - } - - stackPushPtr(pVM->pStack, &pnp_devices); - - return; -} - -void -ficlPnphandlers(FICL_VM *pVM) -{ -#if FICL_ROBUST > 1 - vmCheckStack(pVM, 0, 1); -#endif - - stackPushPtr(pVM->pStack, pnphandlers); - - return; -} - -#endif - -#endif /* ndef TESTMAIN */ - -void ficlCcall(FICL_VM *pVM) { int (*func)(int, ...); int result, p[10]; int nparam, i; #if FICL_ROBUST > 1 vmCheckStack(pVM, 2, 0); #endif func = stackPopPtr(pVM->pStack); nparam = stackPopINT(pVM->pStack); #if FICL_ROBUST > 1 vmCheckStack(pVM, nparam, 1); #endif for (i = 0; i < nparam; i++) p[i] = stackPopINT(pVM->pStack); result = func(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9]); stackPushINT(pVM->pStack, result); return; } void ficlUuidFromString(FICL_VM *pVM) { #ifndef TESTMAIN char *uuid; uint32_t status; #endif char *uuidp; int uuids; uuid_t *u; #if FICL_ROBUST > 1 vmCheckStack(pVM, 2, 0); #endif uuids = stackPopINT(pVM->pStack); uuidp = (char *) stackPopPtr(pVM->pStack); #ifndef TESTMAIN uuid = (char *)ficlMalloc(uuids + 1); if (!uuid) vmThrowErr(pVM, "Error: out of memory"); strncpy(uuid, uuidp, uuids); uuid[uuids] = '\0'; u = (uuid_t *)ficlMalloc(sizeof (*u)); uuid_from_string(uuid, u, &status); ficlFree(uuid); if (status != uuid_s_ok) { ficlFree(u); u = NULL; } #else u = NULL; #endif stackPushPtr(pVM->pStack, u); return; } void ficlUuidToString(FICL_VM *pVM) { #ifndef TESTMAIN char *uuid; uint32_t status; #endif uuid_t *u; #if FICL_ROBUST > 1 vmCheckStack(pVM, 1, 0); #endif u = (uuid_t *)stackPopPtr(pVM->pStack); #ifndef TESTMAIN uuid_to_string(u, &uuid, &status); if (status != uuid_s_ok) { stackPushPtr(pVM->pStack, uuid); stackPushINT(pVM->pStack, strlen(uuid)); } else #endif stackPushINT(pVM->pStack, -1); return; } /************************************************************************** f i c l E x e c F D ** reads in text from file fd and passes it to ficlExec() * returns VM_OUTOFTEXT on success or the ficlExec() error code on * failure. */ #define nLINEBUF 256 int ficlExecFD(FICL_VM *pVM, int fd) { char cp[nLINEBUF]; int nLine = 0, rval = VM_OUTOFTEXT; char ch; CELL id; id = pVM->sourceID; pVM->sourceID.i = fd; /* feed each line to ficlExec */ while (1) { int status, i; i = 0; while ((status = read(fd, &ch, 1)) > 0 && ch != '\n') cp[i++] = ch; nLine++; if (!i) { if (status < 1) break; continue; } rval = ficlExecC(pVM, cp, i); if(rval != VM_QUIT && rval != VM_USEREXIT && rval != VM_OUTOFTEXT) { pVM->sourceID = id; return rval; } } /* ** Pass an empty line with SOURCE-ID == -1 to flush ** any pending REFILLs (as required by FILE wordset) */ pVM->sourceID.i = -1; ficlExec(pVM, ""); pVM->sourceID = id; return rval; } static void displayCellNoPad(FICL_VM *pVM) { CELL c; #if FICL_ROBUST > 1 vmCheckStack(pVM, 1, 0); #endif c = stackPop(pVM->pStack); ltoa((c).i, pVM->pad, pVM->base); vmTextOut(pVM, pVM->pad, 0); return; } /* isdir? - Return whether an fd corresponds to a directory. * * isdir? ( fd -- bool ) */ static void isdirQuestion(FICL_VM *pVM) { struct stat sb; FICL_INT flag; int fd; #if FICL_ROBUST > 1 vmCheckStack(pVM, 1, 1); #endif fd = stackPopINT(pVM->pStack); flag = FICL_FALSE; do { if (fd < 0) break; if (fstat(fd, &sb) < 0) break; if (!S_ISDIR(sb.st_mode)) break; flag = FICL_TRUE; } while (0); stackPushINT(pVM->pStack, flag); } /* fopen - open a file and return new fd on stack. * * fopen ( ptr count mode -- fd ) */ static void pfopen(FICL_VM *pVM) { int mode, fd, count; char *ptr, *name; #if FICL_ROBUST > 1 vmCheckStack(pVM, 3, 1); #endif mode = stackPopINT(pVM->pStack); /* get mode */ count = stackPopINT(pVM->pStack); /* get count */ ptr = stackPopPtr(pVM->pStack); /* get ptr */ if ((count < 0) || (ptr == NULL)) { stackPushINT(pVM->pStack, -1); return; } /* ensure that the string is null terminated */ name = (char *)malloc(count+1); bcopy(ptr,name,count); name[count] = 0; /* open the file */ fd = open(name, mode); free(name); stackPushINT(pVM->pStack, fd); return; } /* fclose - close a file who's fd is on stack. * * fclose ( fd -- ) */ static void pfclose(FICL_VM *pVM) { int fd; #if FICL_ROBUST > 1 vmCheckStack(pVM, 1, 0); #endif fd = stackPopINT(pVM->pStack); /* get fd */ if (fd != -1) close(fd); return; } /* fread - read file contents * * fread ( fd buf nbytes -- nread ) */ static void pfread(FICL_VM *pVM) { int fd, len; char *buf; #if FICL_ROBUST > 1 vmCheckStack(pVM, 3, 1); #endif len = stackPopINT(pVM->pStack); /* get number of bytes to read */ buf = stackPopPtr(pVM->pStack); /* get buffer */ fd = stackPopINT(pVM->pStack); /* get fd */ if (len > 0 && buf && fd != -1) stackPushINT(pVM->pStack, read(fd, buf, len)); else stackPushINT(pVM->pStack, -1); return; } /* freaddir - read directory contents * * freaddir ( fd -- ptr len TRUE | FALSE ) */ static void pfreaddir(FICL_VM *pVM) { #ifdef TESTMAIN static struct dirent dirent; struct stat sb; char *buf; off_t off, ptr; u_int blksz; int bufsz; #endif struct dirent *d; int fd; #if FICL_ROBUST > 1 vmCheckStack(pVM, 1, 3); #endif fd = stackPopINT(pVM->pStack); #if TESTMAIN /* * The readdirfd() function is specific to the loader environment. * We do the best we can to make freaddir work, but it's not at * all guaranteed. */ d = NULL; buf = NULL; do { if (fd == -1) break; if (fstat(fd, &sb) == -1) break; blksz = (sb.st_blksize) ? sb.st_blksize : getpagesize(); if ((blksz & (blksz - 1)) != 0) break; buf = malloc(blksz); if (buf == NULL) break; off = lseek(fd, 0LL, SEEK_CUR); if (off == -1) break; ptr = off; if (lseek(fd, 0, SEEK_SET) == -1) break; bufsz = getdents(fd, buf, blksz); while (bufsz > 0 && bufsz <= ptr) { ptr -= bufsz; bufsz = getdents(fd, buf, blksz); } if (bufsz <= 0) break; d = (void *)(buf + ptr); dirent = *d; off += d->d_reclen; d = (lseek(fd, off, SEEK_SET) != off) ? NULL : &dirent; } while (0); if (buf != NULL) free(buf); #else d = readdirfd(fd); #endif if (d != NULL) { stackPushPtr(pVM->pStack, d->d_name); stackPushINT(pVM->pStack, strlen(d->d_name)); stackPushINT(pVM->pStack, FICL_TRUE); } else { stackPushINT(pVM->pStack, FICL_FALSE); } } /* fload - interpret file contents * * fload ( fd -- ) */ static void pfload(FICL_VM *pVM) { int fd; #if FICL_ROBUST > 1 vmCheckStack(pVM, 1, 0); #endif fd = stackPopINT(pVM->pStack); /* get fd */ if (fd != -1) ficlExecFD(pVM, fd); return; } /* fwrite - write file contents * * fwrite ( fd buf nbytes -- nwritten ) */ static void pfwrite(FICL_VM *pVM) { int fd, len; char *buf; #if FICL_ROBUST > 1 vmCheckStack(pVM, 3, 1); #endif len = stackPopINT(pVM->pStack); /* get number of bytes to read */ buf = stackPopPtr(pVM->pStack); /* get buffer */ fd = stackPopINT(pVM->pStack); /* get fd */ if (len > 0 && buf && fd != -1) stackPushINT(pVM->pStack, write(fd, buf, len)); else stackPushINT(pVM->pStack, -1); return; } /* fseek - seek to a new position in a file * * fseek ( fd ofs whence -- pos ) */ static void pfseek(FICL_VM *pVM) { int fd, pos, whence; #if FICL_ROBUST > 1 vmCheckStack(pVM, 3, 1); #endif whence = stackPopINT(pVM->pStack); pos = stackPopINT(pVM->pStack); fd = stackPopINT(pVM->pStack); stackPushINT(pVM->pStack, lseek(fd, pos, whence)); return; } /* key - get a character from stdin * * key ( -- char ) */ static void key(FICL_VM *pVM) { #if FICL_ROBUST > 1 vmCheckStack(pVM, 0, 1); #endif stackPushINT(pVM->pStack, getchar()); return; } /* key? - check for a character from stdin (FACILITY) * * key? ( -- flag ) */ static void keyQuestion(FICL_VM *pVM) { #if FICL_ROBUST > 1 vmCheckStack(pVM, 0, 1); #endif #ifdef TESTMAIN /* XXX Since we don't fiddle with termios, let it always succeed... */ stackPushINT(pVM->pStack, FICL_TRUE); #else /* But here do the right thing. */ stackPushINT(pVM->pStack, ischar()? FICL_TRUE : FICL_FALSE); #endif return; } /* seconds - gives number of seconds since beginning of time * * beginning of time is defined as: * * BTX - number of seconds since midnight * FreeBSD - number of seconds since Jan 1 1970 * * seconds ( -- u ) */ static void pseconds(FICL_VM *pVM) { #if FICL_ROBUST > 1 vmCheckStack(pVM,0,1); #endif stackPushUNS(pVM->pStack, (FICL_UNS) time(NULL)); return; } /* ms - wait at least that many milliseconds (FACILITY) * * ms ( u -- ) * */ static void ms(FICL_VM *pVM) { #if FICL_ROBUST > 1 vmCheckStack(pVM,1,0); #endif #ifdef TESTMAIN usleep(stackPopUNS(pVM->pStack)*1000); #else delay(stackPopUNS(pVM->pStack)*1000); #endif return; } /* fkey - get a character from a file * * fkey ( file -- char ) */ static void fkey(FICL_VM *pVM) { int i, fd; char ch; #if FICL_ROBUST > 1 vmCheckStack(pVM, 1, 1); #endif fd = stackPopINT(pVM->pStack); i = read(fd, &ch, 1); stackPushINT(pVM->pStack, i > 0 ? ch : -1); return; } -#ifdef __i386__ /* - * pcibios-device-count (devid -- count) - * - * Returns the PCI BIOS' count of how many devices matching devid are in the system. - * devid is the 32-bit vendor + device. - */ -static void -ficlPciBiosCountDevices(FICL_VM *pVM) -{ - uint32_t devid; - int i; - - devid = stackPopINT(pVM->pStack); - - i = biospci_count_device_type(devid); - - stackPushINT(pVM->pStack, i); -} - -/* - * pcibios-write-config (locator offset width value -- ) - * - * Writes the specified config register. - * Locator is bus << 8 | device << 3 | fuction - * offset is the pci config register - * width is 0 for byte, 1 for word, 2 for dword - * value is the value to write - */ -static void -ficlPciBiosWriteConfig(FICL_VM *pVM) -{ - uint32_t value, width, offset, locator; - - value = stackPopINT(pVM->pStack); - width = stackPopINT(pVM->pStack); - offset = stackPopINT(pVM->pStack); - locator = stackPopINT(pVM->pStack); - - biospci_write_config(locator, offset, width, value); -} - -/* - * pcibios-read-config (locator offset width -- value) - * - * Reads the specified config register. - * Locator is bus << 8 | device << 3 | fuction - * offset is the pci config register - * width is 0 for byte, 1 for word, 2 for dword - * value is the value to read from the register - */ -static void -ficlPciBiosReadConfig(FICL_VM *pVM) -{ - uint32_t value, width, offset, locator; - - width = stackPopINT(pVM->pStack); - offset = stackPopINT(pVM->pStack); - locator = stackPopINT(pVM->pStack); - - biospci_read_config(locator, offset, width, &value); - - stackPushINT(pVM->pStack, value); -} - -/* - * pcibios-find-devclass (class index -- locator) - * - * Finds the index'th instance of class in the pci tree. - * must be an exact match. - * class is the class to search for. - * index 0..N (set to 0, increment until error) - * - * Locator is bus << 8 | device << 3 | fuction (or -1 on error) - */ -static void -ficlPciBiosFindDevclass(FICL_VM *pVM) -{ - uint32_t index, class, locator; - - index = stackPopINT(pVM->pStack); - class = stackPopINT(pVM->pStack); - - if (biospci_find_devclass(class, index, &locator)) - locator = 0xffffffff; - - stackPushINT(pVM->pStack, locator); -} - -/* - * pcibios-find-device(devid index -- locator) - * - * Finds the index'th instance of devid in the pci tree. - * must be an exact match. - * class is the class to search for. - * index 0..N (set to 0, increment until error) - * - * Locator is bus << 8 | device << 3 | fuction (or -1 on error) - */ -static void -ficlPciBiosFindDevice(FICL_VM *pVM) -{ - uint32_t index, devid, locator; - - index = stackPopINT(pVM->pStack); - devid = stackPopINT(pVM->pStack); - - if (biospci_find_device(devid, index, &locator)) - locator = 0xffffffff; - - stackPushINT(pVM->pStack, locator); -} - -/* - * pcibios-find-device(bus device function -- locator) - * - * converts bus, device, function to locator. - * - * Locator is bus << 8 | device << 3 | fuction - */ -static void -ficlPciBiosLocator(FICL_VM *pVM) -{ - uint32_t bus, device, function, locator; - - function = stackPopINT(pVM->pStack); - device = stackPopINT(pVM->pStack); - bus = stackPopINT(pVM->pStack); - - locator = biospci_locator(bus, device, function); - - stackPushINT(pVM->pStack, locator); -} -#endif - -/* ** Retrieves free space remaining on the dictionary */ static void freeHeap(FICL_VM *pVM) { stackPushINT(pVM->pStack, dictCellsAvail(ficlGetDict(pVM->pSys))); } /******************* Increase dictionary size on-demand ******************/ static void ficlDictThreshold(FICL_VM *pVM) { stackPushPtr(pVM->pStack, &dictThreshold); } static void ficlDictIncrease(FICL_VM *pVM) { stackPushPtr(pVM->pStack, &dictIncrease); } /************************************************************************** f i c l C o m p i l e P l a t f o r m ** Build FreeBSD platform extensions into the system dictionary **************************************************************************/ void ficlCompilePlatform(FICL_SYSTEM *pSys) { + ficlCompileFcn **fnpp; FICL_DICT *dp = pSys->dp; assert (dp); dictAppendWord(dp, ".#", displayCellNoPad, FW_DEFAULT); dictAppendWord(dp, "isdir?", isdirQuestion, FW_DEFAULT); dictAppendWord(dp, "fopen", pfopen, FW_DEFAULT); dictAppendWord(dp, "fclose", pfclose, FW_DEFAULT); dictAppendWord(dp, "fread", pfread, FW_DEFAULT); dictAppendWord(dp, "freaddir", pfreaddir, FW_DEFAULT); dictAppendWord(dp, "fload", pfload, FW_DEFAULT); dictAppendWord(dp, "fkey", fkey, FW_DEFAULT); dictAppendWord(dp, "fseek", pfseek, FW_DEFAULT); dictAppendWord(dp, "fwrite", pfwrite, FW_DEFAULT); dictAppendWord(dp, "key", key, FW_DEFAULT); dictAppendWord(dp, "key?", keyQuestion, FW_DEFAULT); dictAppendWord(dp, "ms", ms, FW_DEFAULT); dictAppendWord(dp, "seconds", pseconds, FW_DEFAULT); dictAppendWord(dp, "heap?", freeHeap, FW_DEFAULT); dictAppendWord(dp, "dictthreshold", ficlDictThreshold, FW_DEFAULT); dictAppendWord(dp, "dictincrease", ficlDictIncrease, FW_DEFAULT); dictAppendWord(dp, "setenv", ficlSetenv, FW_DEFAULT); dictAppendWord(dp, "setenv?", ficlSetenvq, FW_DEFAULT); dictAppendWord(dp, "getenv", ficlGetenv, FW_DEFAULT); dictAppendWord(dp, "unsetenv", ficlUnsetenv, FW_DEFAULT); dictAppendWord(dp, "copyin", ficlCopyin, FW_DEFAULT); dictAppendWord(dp, "copyout", ficlCopyout, FW_DEFAULT); dictAppendWord(dp, "findfile", ficlFindfile, FW_DEFAULT); dictAppendWord(dp, "ccall", ficlCcall, FW_DEFAULT); dictAppendWord(dp, "uuid-from-string", ficlUuidFromString, FW_DEFAULT); dictAppendWord(dp, "uuid-to-string", ficlUuidToString, FW_DEFAULT); -#ifndef TESTMAIN -#ifdef __i386__ - dictAppendWord(dp, "outb", ficlOutb, FW_DEFAULT); - dictAppendWord(dp, "inb", ficlInb, FW_DEFAULT); -#endif -#ifdef HAVE_PNP - dictAppendWord(dp, "pnpdevices",ficlPnpdevices, FW_DEFAULT); - dictAppendWord(dp, "pnphandlers",ficlPnphandlers, FW_DEFAULT); -#endif -#endif -#ifdef __i386__ - dictAppendWord(dp, "pcibios-device-count", ficlPciBiosCountDevices, FW_DEFAULT); - dictAppendWord(dp, "pcibios-read-config", ficlPciBiosReadConfig, FW_DEFAULT); - dictAppendWord(dp, "pcibios-write-config", ficlPciBiosWriteConfig, FW_DEFAULT); - dictAppendWord(dp, "pcibios-find-devclass", ficlPciBiosFindDevclass, FW_DEFAULT); - dictAppendWord(dp, "pcibios-find-device", ficlPciBiosFindDevice, FW_DEFAULT); - dictAppendWord(dp, "pcibios-locator", ficlPciBiosLocator, FW_DEFAULT); -#endif + + SET_FOREACH(fnpp, Xficl_compile_set) { + (*fnpp)(pSys); + } #if defined(PC98) ficlSetEnv(pSys, "arch-pc98", FICL_TRUE); #elif defined(__i386__) ficlSetEnv(pSys, "arch-i386", FICL_TRUE); ficlSetEnv(pSys, "arch-powerpc", FICL_FALSE); #elif defined(__powerpc__) ficlSetEnv(pSys, "arch-i386", FICL_FALSE); ficlSetEnv(pSys, "arch-powerpc", FICL_TRUE); #endif return; } Index: head/sys/boot/i386/libi386/Makefile =================================================================== --- head/sys/boot/i386/libi386/Makefile (revision 307323) +++ head/sys/boot/i386/libi386/Makefile (revision 307324) @@ -1,81 +1,82 @@ # $FreeBSD$ # LIB= i386 INTERNALLIB= SRCS= biosacpi.c bioscd.c biosdisk.c biosmem.c biospnp.c \ biospci.c biossmap.c bootinfo.c bootinfo32.c bootinfo64.c \ comconsole.c devicename.c elf32_freebsd.c \ elf64_freebsd.c multiboot.c multiboot_tramp.S \ i386_copy.c i386_module.c nullconsole.c pxe.c pxetramp.s \ smbios.c time.c vidconsole.c amd64_tramp.S spinconsole.c .PATH: ${.CURDIR}/../../zfs SRCS+= devicename_stubs.c # Enable PXE TFTP or NFS support, not both. .if defined(LOADER_TFTP_SUPPORT) CFLAGS+= -DLOADER_TFTP_SUPPORT .else CFLAGS+= -DLOADER_NFS_SUPPORT .endif BOOT_COMCONSOLE_PORT?= 0x3f8 CFLAGS+= -DCOMPORT=${BOOT_COMCONSOLE_PORT} BOOT_COMCONSOLE_SPEED?= 9600 CFLAGS+= -DCOMSPEED=${BOOT_COMCONSOLE_SPEED} .ifdef(BOOT_BIOSDISK_DEBUG) # Make the disk code more talkative CFLAGS+= -DDISK_DEBUG .endif .if !defined(LOADER_NO_GELI_SUPPORT) # Decrypt encrypted drives CFLAGS+= -DLOADER_GELI_SUPPORT CFLAGS+= -I${.CURDIR}/../../geli .endif .if !defined(BOOT_HIDE_SERIAL_NUMBERS) # Export serial numbers, UUID, and asset tag from loader. CFLAGS+= -DSMBIOS_SERIAL_NUMBERS .if defined(BOOT_LITTLE_ENDIAN_UUID) # Use little-endian UUID format as defined in SMBIOS 2.6. CFLAGS+= -DSMBIOS_LITTLE_ENDIAN_UUID .elif defined(BOOT_NETWORK_ENDIAN_UUID) # Use network-endian UUID format for backward compatibility. CFLAGS+= -DSMBIOS_NETWORK_ENDIAN_UUID .endif .endif # Include simple terminal emulation (cons25-compatible) CFLAGS+= -DTERM_EMU # XXX: make alloca() useable CFLAGS+= -Dalloca=__builtin_alloca -CFLAGS+= -I${.CURDIR}/../../common -I${.CURDIR}/../common \ +CFLAGS+= -I${.CURDIR}/../../ficl -I${.CURDIR}/../../ficl/i386 \ + -I${.CURDIR}/../../common -I${.CURDIR}/../common \ -I${.CURDIR}/../btx/lib \ -I${.CURDIR}/../../../contrib/dev/acpica/include \ -I${.CURDIR}/../../.. -I. # the location of libstand CFLAGS+= -I${.CURDIR}/../../../../lib/libstand/ # Handle FreeBSD specific %b and %D printf format specifiers CFLAGS+= ${FORMAT_EXTENSIONS} .if ${MACHINE_CPUARCH} == "amd64" CLEANFILES+= machine machine: .NOMETA ln -sf ${.CURDIR}/../../../i386/include machine .endif .include # XXX: clang integrated-as doesn't grok .codeNN directives yet CFLAGS.amd64_tramp.S= ${CLANG_NO_IAS} CFLAGS.multiboot_tramp.S= ${CLANG_NO_IAS} .if ${MACHINE_CPUARCH} == "amd64" beforedepend ${OBJS}: machine .endif Index: head/sys/boot/i386/libi386/biospci.c =================================================================== --- head/sys/boot/i386/libi386/biospci.c (revision 307323) +++ head/sys/boot/i386/libi386/biospci.c (revision 307324) @@ -1,428 +1,582 @@ /*- * Copyright (c) 1998 Michael Smith + * Copyright (c) 2016 Netflix, Inc * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); /* * PnP enumerator using the PCI BIOS. */ #include #include #include #include #include #include "libi386.h" +#include "ficl.h" /* * Stupid PCI BIOS interface doesn't let you simply enumerate everything * that's there, instead you have to ask it if it has something. * * So we have to scan by class code, subclass code and sometimes programming * interface. */ struct pci_progif { int pi_code; const char *pi_name; }; static struct pci_progif progif_null[] = { {0x0, NULL}, {-1, NULL} }; static struct pci_progif progif_display[] = { {0x0, "VGA"}, {0x1, "8514"}, {-1, NULL} }; static struct pci_progif progif_ide[] = { {0x00, NULL}, {0x01, NULL}, {0x02, NULL}, {0x03, NULL}, {0x04, NULL}, {0x05, NULL}, {0x06, NULL}, {0x07, NULL}, {0x08, NULL}, {0x09, NULL}, {0x0a, NULL}, {0x0b, NULL}, {0x0c, NULL}, {0x0d, NULL}, {0x0e, NULL}, {0x0f, NULL}, {0x80, NULL}, {0x81, NULL}, {0x82, NULL}, {0x83, NULL}, {0x84, NULL}, {0x85, NULL}, {0x86, NULL}, {0x87, NULL}, {0x88, NULL}, {0x89, NULL}, {0x8a, NULL}, {0x8b, NULL}, {0x8c, NULL}, {0x8d, NULL}, {0x8e, NULL}, {0x8f, NULL}, {-1, NULL} }; static struct pci_progif progif_serial[] = { {0x0, "8250"}, {0x1, "16450"}, {0x2, "16550"}, {-1, NULL} }; static struct pci_progif progif_parallel[] = { {0x0, "Standard"}, {0x1, "Bidirectional"}, {0x2, "ECP"}, {-1, NULL} }; static struct pci_progif progif_firewire[] = { {0x10, "OHCI"}, {-1, NULL} }; struct pci_subclass { int ps_subclass; const char *ps_name; struct pci_progif *ps_progif; /* if set, use for programming interface value(s) */ }; static struct pci_subclass subclass_old[] = { {0x0, "Old non-VGA", progif_null}, {0x1, "Old VGA", progif_null}, {-1, NULL, NULL} }; static struct pci_subclass subclass_mass[] = { {0x0, "SCSI", progif_null}, {0x1, "IDE", progif_ide}, {0x2, "Floppy disk", progif_null}, {0x3, "IPI", progif_null}, {0x4, "RAID", progif_null}, {0x80, "mass storage", progif_null}, {-1, NULL, NULL} }; static struct pci_subclass subclass_net[] = { {0x0, "Ethernet", progif_null}, {0x1, "Token ring", progif_null}, {0x2, "FDDI", progif_null}, {0x3, "ATM", progif_null}, {0x80, "network", progif_null}, {-1, NULL, NULL} }; static struct pci_subclass subclass_display[] = { {0x0, NULL, progif_display}, {0x1, "XGA", progif_null}, {0x80, "other", progif_null}, {-1, NULL, NULL} }; static struct pci_subclass subclass_comms[] = { {0x0, "serial", progif_serial}, {0x1, "parallel", progif_parallel}, {0x80, "communications", progif_null}, {-1, NULL, NULL} }; static struct pci_subclass subclass_serial[] = { {0x0, "FireWire", progif_firewire}, {0x1, "ACCESS.bus", progif_null}, {0x2, "SSA", progif_null}, {0x3, "USB", progif_null}, {0x4, "Fibrechannel", progif_null}, {-1, NULL, NULL} }; static struct pci_class { int pc_class; const char *pc_name; struct pci_subclass *pc_subclass; } pci_classes[] = { {0x0, "device", subclass_old}, {0x1, "controller", subclass_mass}, {0x2, "controller", subclass_net}, {0x3, "display", subclass_display}, {0x7, "controller", subclass_comms}, {0xc, "controller", subclass_serial}, {-1, NULL, NULL} }; static void biospci_enumerate(void); static void biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi); struct pnphandler biospcihandler = { "PCI BIOS", biospci_enumerate }; #define PCI_BIOS_PRESENT 0xb101 #define FIND_PCI_DEVICE 0xb102 #define FIND_PCI_CLASS_CODE 0xb103 #define GENERATE_SPECIAL_CYCLE 0xb106 #define READ_CONFIG_BYTE 0xb108 #define READ_CONFIG_WORD 0xb109 #define READ_CONFIG_DWORD 0xb10a #define WRITE_CONFIG_BYTE 0xb10b #define WRITE_CONFIG_WORD 0xb10c #define WRITE_CONFIG_DWORD 0xb10d #define GET_IRQ_ROUTING_OPTIONS 0xb10e #define SET_PCI_IRQ 0xb10f #define PCI_INT 0x1a #define PCI_SIGNATURE 0x20494350 /* AKA "PCI " */ void biospci_detect(void) { uint16_t version, hwcap, maxbus; char buf[24]; /* Find the PCI BIOS */ v86.ctl = V86_FLAGS; v86.addr = PCI_INT; v86.eax = PCI_BIOS_PRESENT; v86.edi = 0x0; v86int(); /* Check for OK response */ if (V86_CY(v86.efl) || ((v86.eax & 0xff00) != 0) || (v86.edx != PCI_SIGNATURE)) return; version = v86.ebx & 0xffff; hwcap = v86.eax & 0xff; maxbus = v86.ecx & 0xff; #if 0 printf("PCI BIOS %d.%d%s%s maxbus %d\n", bcd2bin((version >> 8) & 0xf), bcd2bin(version & 0xf), (hwcap & 1) ? " config1" : "", (hwcap & 2) ? " config2" : "", maxbus); #endif sprintf(buf, "%d", bcd2bin((version >> 8) & 0xf)); setenv("pcibios.major", buf, 1); sprintf(buf, "%d", bcd2bin(version & 0xf)); setenv("pcibios.minor", buf, 1); sprintf(buf, "%d", !!(hwcap & 1)); setenv("pcibios.config1", buf, 1); sprintf(buf, "%d", !!(hwcap & 2)); setenv("pcibios.config2", buf, 1); sprintf(buf, "%d", maxbus); setenv("pcibios.maxbus", buf, 1); } static void biospci_enumerate(void) { int device_index, err; uint32_t locator, devid; struct pci_class *pc; struct pci_subclass *psc; struct pci_progif *ppi; /* Iterate over known classes */ for (pc = pci_classes; pc->pc_class >= 0; pc++) { /* Iterate over subclasses */ for (psc = pc->pc_subclass; psc->ps_subclass >= 0; psc++) { /* Iterate over programming interfaces */ for (ppi = psc->ps_progif; ppi->pi_code >= 0; ppi++) { /* Scan for matches */ for (device_index = 0; ; device_index++) { /* Look for a match */ err = biospci_find_devclass((pc->pc_class << 16) + (psc->ps_subclass << 8) + ppi->pi_code, device_index, &locator); if (err != 0) break; /* Read the device identifier from the nominated device */ err = biospci_read_config(locator, 0, 2, &devid); if (err != 0) break; /* We have the device ID, create a PnP object and save everything */ biospci_addinfo(devid, pc, psc, ppi); } } } } } static void biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi) { struct pnpinfo *pi; char desc[80]; /* build the description */ desc[0] = 0; if (ppi->pi_name != NULL) { strcat(desc, ppi->pi_name); strcat(desc, " "); } if (psc->ps_name != NULL) { strcat(desc, psc->ps_name); strcat(desc, " "); } if (pc->pc_name != NULL) strcat(desc, pc->pc_name); pi = pnp_allocinfo(); pi->pi_desc = strdup(desc); sprintf(desc,"0x%08x", devid); pnp_addident(pi, desc); pnp_addinfo(pi); } int biospci_find_devclass(uint32_t class, int index, uint32_t *locator) { v86.ctl = V86_FLAGS; v86.addr = PCI_INT; v86.eax = FIND_PCI_CLASS_CODE; v86.ecx = class; v86.esi = index; v86int(); /* error */ if (V86_CY(v86.efl) || (v86.eax & 0xff00)) return (-1); *locator = v86.ebx; return (0); } -int +static int biospci_find_device(uint32_t devid, int index, uint32_t *locator) { v86.ctl = V86_FLAGS; v86.addr = PCI_INT; v86.eax = FIND_PCI_DEVICE; v86.edx = devid & 0xffff; /* EDX - Vendor ID */ v86.ecx = (devid >> 16) & 0xffff; /* ECX - Device ID */ v86.esi = index; v86int(); /* error */ if (V86_CY(v86.efl) || (v86.eax & 0xff00)) return (-1); *locator = v86.ebx; return (0); } /* * Configuration space access methods. * width = 0(byte), 1(word) or 2(dword). */ int biospci_write_config(uint32_t locator, int offset, int width, uint32_t val) { v86.ctl = V86_FLAGS; v86.addr = PCI_INT; v86.eax = WRITE_CONFIG_BYTE + width; v86.ebx = locator; v86.edi = offset; v86.ecx = val; v86int(); /* error */ if (V86_CY(v86.efl) || (v86.eax & 0xff00)) return (-1); return(0); } int biospci_read_config(uint32_t locator, int offset, int width, uint32_t *val) { v86.ctl = V86_FLAGS; v86.addr = PCI_INT; v86.eax = READ_CONFIG_BYTE + width; v86.ebx = locator; v86.edi = offset; v86int(); /* error */ if (V86_CY(v86.efl) || (v86.eax & 0xff00)) return (-1); *val = v86.ecx; return (0); } uint32_t biospci_locator(int8_t bus, uint8_t device, uint8_t function) { return ((bus << 8) | ((device & 0x1f) << 3) | (function & 0x7)); } /* * Counts the number of instances of devid we have in the system, as least as * far as the PCI BIOS is able to tell. */ -int +static int biospci_count_device_type(uint32_t devid) { int i; for (i = 0; 1; i++) { v86.ctl = V86_FLAGS; v86.addr = PCI_INT; v86.eax = FIND_PCI_DEVICE; v86.edx = devid & 0xffff; /* EDX - Vendor ID */ v86.ecx = (devid >> 16) & 0xffff; /* ECX - Device ID */ v86.esi = i; v86int(); if (V86_CY(v86.efl) || (v86.eax & 0xff00)) break; } return i; } + +/* + * pcibios-device-count (devid -- count) + * + * Returns the PCI BIOS' count of how many devices matching devid are in the system. + * devid is the 32-bit vendor + device. + */ +static void +ficlPciBiosCountDevices(FICL_VM *pVM) +{ + uint32_t devid; + int i; + + devid = stackPopINT(pVM->pStack); + + i = biospci_count_device_type(devid); + + stackPushINT(pVM->pStack, i); +} + +/* + * pcibios-write-config (locator offset width value -- ) + * + * Writes the specified config register. + * Locator is bus << 8 | device << 3 | fuction + * offset is the pci config register + * width is 0 for byte, 1 for word, 2 for dword + * value is the value to write + */ +static void +ficlPciBiosWriteConfig(FICL_VM *pVM) +{ + uint32_t value, width, offset, locator; + + value = stackPopINT(pVM->pStack); + width = stackPopINT(pVM->pStack); + offset = stackPopINT(pVM->pStack); + locator = stackPopINT(pVM->pStack); + + biospci_write_config(locator, offset, width, value); +} + +/* + * pcibios-read-config (locator offset width -- value) + * + * Reads the specified config register. + * Locator is bus << 8 | device << 3 | fuction + * offset is the pci config register + * width is 0 for byte, 1 for word, 2 for dword + * value is the value to read from the register + */ +static void +ficlPciBiosReadConfig(FICL_VM *pVM) +{ + uint32_t value, width, offset, locator; + + width = stackPopINT(pVM->pStack); + offset = stackPopINT(pVM->pStack); + locator = stackPopINT(pVM->pStack); + + biospci_read_config(locator, offset, width, &value); + + stackPushINT(pVM->pStack, value); +} + +/* + * pcibios-find-devclass (class index -- locator) + * + * Finds the index'th instance of class in the pci tree. + * must be an exact match. + * class is the class to search for. + * index 0..N (set to 0, increment until error) + * + * Locator is bus << 8 | device << 3 | fuction (or -1 on error) + */ +static void +ficlPciBiosFindDevclass(FICL_VM *pVM) +{ + uint32_t index, class, locator; + + index = stackPopINT(pVM->pStack); + class = stackPopINT(pVM->pStack); + + if (biospci_find_devclass(class, index, &locator)) + locator = 0xffffffff; + + stackPushINT(pVM->pStack, locator); +} + +/* + * pcibios-find-device(devid index -- locator) + * + * Finds the index'th instance of devid in the pci tree. + * must be an exact match. + * class is the class to search for. + * index 0..N (set to 0, increment until error) + * + * Locator is bus << 8 | device << 3 | fuction (or -1 on error) + */ +static void +ficlPciBiosFindDevice(FICL_VM *pVM) +{ + uint32_t index, devid, locator; + + index = stackPopINT(pVM->pStack); + devid = stackPopINT(pVM->pStack); + + if (biospci_find_device(devid, index, &locator)) + locator = 0xffffffff; + + stackPushINT(pVM->pStack, locator); +} + +/* + * pcibios-find-device(bus device function -- locator) + * + * converts bus, device, function to locator. + * + * Locator is bus << 8 | device << 3 | fuction + */ +static void +ficlPciBiosLocator(FICL_VM *pVM) +{ + uint32_t bus, device, function, locator; + + function = stackPopINT(pVM->pStack); + device = stackPopINT(pVM->pStack); + bus = stackPopINT(pVM->pStack); + + locator = biospci_locator(bus, device, function); + + stackPushINT(pVM->pStack, locator); +} + +/* + * Glue function to add the appropriate forth words to access pci bios + * functionality. + */ +static void ficlCompilePciBios(FICL_SYSTEM *pSys) +{ + FICL_DICT *dp = pSys->dp; + assert (dp); + + dictAppendWord(dp, "pcibios-device-count", ficlPciBiosCountDevices, FW_DEFAULT); + dictAppendWord(dp, "pcibios-read-config", ficlPciBiosReadConfig, FW_DEFAULT); + dictAppendWord(dp, "pcibios-write-config", ficlPciBiosWriteConfig, FW_DEFAULT); + dictAppendWord(dp, "pcibios-find-devclass", ficlPciBiosFindDevclass, FW_DEFAULT); + dictAppendWord(dp, "pcibios-find-device", ficlPciBiosFindDevice, FW_DEFAULT); + dictAppendWord(dp, "pcibios-locator", ficlPciBiosLocator, FW_DEFAULT); +} + +FICL_COMPILE_SET(ficlCompilePciBios); Index: head/sys/boot/i386/libi386/libi386.h =================================================================== --- head/sys/boot/i386/libi386/libi386.h (revision 307323) +++ head/sys/boot/i386/libi386/libi386.h (revision 307324) @@ -1,126 +1,124 @@ /*- * Copyright (c) 1998 Michael Smith * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ /* * i386 fully-qualified device descriptor. * Note, this must match the 'struct devdesc' declaration * in bootstrap.h and also with struct zfs_devdesc for zfs * support. */ struct i386_devdesc { struct devsw *d_dev; int d_type; int d_unit; union { struct { void *data; int slice; int partition; off_t offset; } biosdisk; struct { void *data; } bioscd; struct { void *data; uint64_t pool_guid; uint64_t root_guid; } zfs; } d_kind; }; int i386_getdev(void **vdev, const char *devspec, const char **path); char *i386_fmtdev(void *vdev); int i386_setcurrdev(struct env_var *ev, int flags, const void *value); extern struct devdesc currdev; /* our current device */ #define MAXDEV 31 /* maximum number of distinct devices */ #define MAXBDDEV MAXDEV /* exported devices XXX rename? */ extern struct devsw bioscd; extern struct devsw biosdisk; extern struct devsw pxedisk; extern struct fs_ops pxe_fsops; int bc_add(int biosdev); /* Register CD booted from. */ int bc_getdev(struct i386_devdesc *dev); /* return dev_t for (dev) */ int bc_bios2unit(int biosdev); /* xlate BIOS device -> bioscd unit */ int bc_unit2bios(int unit); /* xlate bioscd unit -> BIOS device */ uint32_t bd_getbigeom(int bunit); /* return geometry in bootinfo format */ int bd_bios2unit(int biosdev); /* xlate BIOS device -> biosdisk unit */ int bd_unit2bios(int unit); /* xlate biosdisk unit -> BIOS device */ int bd_getdev(struct i386_devdesc *dev); /* return dev_t for (dev) */ ssize_t i386_copyin(const void *src, vm_offset_t dest, const size_t len); ssize_t i386_copyout(const vm_offset_t src, void *dest, const size_t len); ssize_t i386_readin(const int fd, vm_offset_t dest, const size_t len); struct preloaded_file; void bios_addsmapdata(struct preloaded_file *); void bios_getsmap(void); void bios_getmem(void); extern uint32_t bios_basemem; /* base memory in bytes */ extern uint32_t bios_extmem; /* extended memory in bytes */ extern vm_offset_t memtop; /* last address of physical memory + 1 */ extern vm_offset_t memtop_copyin; /* memtop less heap size for the cases */ /* when heap is at the top of */ /* extended memory; for other cases */ /* just the same as memtop */ extern uint32_t high_heap_size; /* extended memory region available */ extern vm_offset_t high_heap_base; /* for use as the heap */ void biospci_detect(void); -int biospci_count_device_type(uint32_t devid); -int biospci_find_devclass(uint32_t class, int index, uint32_t *locator); -int biospci_find_device(uint32_t devid, int index, uint32_t *locator); -int biospci_write_config(uint32_t locator, int offset, int width, uint32_t val); -int biospci_read_config(uint32_t locator, int offset, int width, uint32_t *val); +int biospci_find_devclass(uint32_t class, int index, uint32_t *locator); +int biospci_read_config(uint32_t locator, int offset, int width, uint32_t *val); uint32_t biospci_locator(int8_t bus, uint8_t device, uint8_t function); +int biospci_write_config(uint32_t locator, int offset, int width, uint32_t val); void biosacpi_detect(void); int i386_autoload(void); int bi_getboothowto(char *kargs); void bi_setboothowto(int howto); vm_offset_t bi_copyenv(vm_offset_t addr); int bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t *modulep, vm_offset_t *kernend); int bi_load64(char *args, vm_offset_t addr, vm_offset_t *modulep, vm_offset_t *kernend, int add_smap); void pxe_enable(void *pxeinfo); Index: head/sys/boot/mips/beri/loader/loader.ldscript =================================================================== --- head/sys/boot/mips/beri/loader/loader.ldscript (revision 307323) +++ head/sys/boot/mips/beri/loader/loader.ldscript (revision 307324) @@ -1,81 +1,85 @@ /*- * Copyright (c) 2011-2014 Robert N. M. Watson * All rights reserved. * * This software was developed by SRI International and the University of * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) * ("CTSRD"), as part of the DARPA CRASH research programme. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ INCLUDE ../common/common.ldscript /* * Location where loader will execute. */ __loader_base__ = 0x20000; __loader_base_vaddr__ = __mips64_xkphys_cached__ + __loader_base__; /* * Highest address the loader is allowed to use below the kernel. */ __loader_end__ = 0x100000; __loader_end_vaddr__ = __mips64_xkphys_cached__ + __loader_end__; OUTPUT_FORMAT("elf64-tradbigmips"); OUTPUT_ARCH(mips) ENTRY(start) SECTIONS { /* * We rely on boot2 having (a) configured a stack, and (b) loaded us * to an appropriate bit of physical/virtual memory such that no * self-relocating code is required here. */ . = __loader_base_vaddr__; . += SIZEOF_HEADERS; .text ALIGN(0x10): { start.o(.text*) *(EXCLUDE_FILE (start.o) .text*) *(.rodata*) __start_set_Xcommand_set = .; KEEP(*(set_Xcommand_set)) __stop_set_Xcommand_set = .; + + __start_set_Xficl_compile_set = .; + KEEP(*(set_Xficl_compile_set)) + __stop_set_Xficl_compile_set = .; } .data ALIGN(0x10): { *(.data*)} .bss ALIGN(0x10): { *(.bss*) } __heap = ALIGN(0x8); /* 64-bit aligned heap pointer */ __data_end = .; __boot_loader_len__ = . - __loader_base_vaddr__; __bss_start = ADDR(.bss); __bss_end = ALIGN(__bss_start + SIZEOF(.bss), 0x8); __heap_start = .; __heap_end = __loader_end_vaddr__; __heap_len = __heap_end - __heap_start; } Index: head/sys/boot/pc98/libpc98/Makefile =================================================================== --- head/sys/boot/pc98/libpc98/Makefile (revision 307323) +++ head/sys/boot/pc98/libpc98/Makefile (revision 307324) @@ -1,50 +1,51 @@ # $FreeBSD$ # LIB= pc98 INTERNALLIB= .PATH: ${.CURDIR}/../../i386/libi386 SRCS= bioscd.c biosdisk.c biosmem.c biospnp.c \ biospci.c biossmap.c bootinfo.c bootinfo32.c \ comconsole.c devicename.c elf32_freebsd.c \ i386_copy.c i386_module.c nullconsole.c pc98_sys.c pxe.c pxetramp.s \ time.c vidconsole.c .PATH: ${.CURDIR}/../../zfs SRCS+= devicename_stubs.c # Enable PXE TFTP or NFS support, not both. .if defined(LOADER_TFTP_SUPPORT) CFLAGS+= -DLOADER_TFTP_SUPPORT .else CFLAGS+= -DLOADER_NFS_SUPPORT .endif BOOT_COMCONSOLE_PORT?= 0x238 CFLAGS+= -DCOMPORT=${BOOT_COMCONSOLE_PORT} BOOT_COMCONSOLE_SPEED?= 9600 CFLAGS+= -DCOMSPEED=${BOOT_COMCONSOLE_SPEED} .ifdef(BOOT_BIOSDISK_DEBUG) # Make the disk code more talkative CFLAGS+= -DDISK_DEBUG .endif # Include simple terminal emulation (cons25-compatible) CFLAGS+= -DTERM_EMU # XXX: make alloca() useable CFLAGS+= -Dalloca=__builtin_alloca -CFLAGS+= -I${.CURDIR}/../../common \ +CFLAGS+= -I${.CURDIR}/../../ficl -I${.CURDIR}/../../ficl/i386 \ + -I${.CURDIR}/../../common \ -I${.CURDIR}/../btx/lib \ -I${.CURDIR}/../../i386/libi386 \ -I${.CURDIR}/../../.. -I. # the location of libstand CFLAGS+= -I${.CURDIR}/../../../../lib/libstand/ # Handle FreeBSD specific %b and %D printf format specifiers CFLAGS+= ${FORMAT_EXTENSIONS} .include