mips64emul |
This page describes some of the internals of mips64emul. For more general documentation, please read the User Documentation.
In reality, a lot of things need to be handled. Before each instruction is executed, the emulator checks to see if any interrupts are asserted which are not masked away. If so, then an INT exception is generated. Exceptions cause the program counter to be set to a specific value, and some of the system coprocessor's registers to be set to values signifying what kind of exception it was (an interrupt exception in this case).
Reading instructions from memory is done through a TLB, a translation lookaside buffer. The TLB on MIPS is software controlled, which means that the program running inside the emulator (for example an operating system kernel) has to take care of manually updating the TLB. Some memory addresses are translated into physical addresses directly, some are translated into valid physical addresses via the TLB, and some memory references are not valid. Invalid memory references cause exceptions.
After an instruction has been read from memory, the emulator checks which opcode it contains and executes the instruction. Executing an instruction usually involves reading some register and writing some register, or perhaps a load from memory (or a store to memory). The program counter is increased for every instruction.
Some memory references point to physical addresses which are not in the normal RAM address space. They may point to hardware devices. If that is the case, then loads and stores are converted into calls to a device access function. The device access function is then responsible for handling these reads and writes. For example, a graphical framebuffer device may put a pixel on the screen when a value is written to it, or a serial controller device may output a character to stdout when written to.
As far as I have understood it, there seems to be two different ways to go:
or
Since I have choosen the second kind of implementation, I have to write support explicitly for any kind of network protocol that should be supported. As of 2004-07-09, the following has been implemented and seems to work under at least NetBSD/pmax and OpenBSD/pmax under DECstation -D2 emulation:
Inside emulated NetBSD or OpenBSD, running the following commands should configure the emulated NIC:
# ifconfig le0 10.0.0.1 # route add default 10.0.0.254 add net default: gateway 10.0.0.254If you want nameserver lookups to work, you need a valid /etc/resolv.conf as well:
# echo nameserver 129.16.1.3 > /etc/resolv.conf(But replace 129.16.1.3 with the actual real-world IP address of your nearest nameserver.)
Now, host lookups should work:
# host -a www.netbsd.org Trying null domain rcode = 0 (Success), ancount=2 The following answer is not authoritative: The following answer is not verified as authentic by the server: www.netbsd.org 86400 IN AAAA 2001:4f8:4:7:290:27ff:feab:19a7 www.netbsd.org 86400 IN A 204.152.184.116 For authoritative answers, see: netbsd.org 83627 IN NS uucp-gw-2.pa.dec.com netbsd.org 83627 IN NS ns.netbsd.org netbsd.org 83627 IN NS adns1.berkeley.edu netbsd.org 83627 IN NS adns2.berkeley.edu netbsd.org 83627 IN NS uucp-gw-1.pa.dec.com Additional information: ns.netbsd.org 83627 IN A 204.152.184.164 uucp-gw-1.pa.dec.com 172799 IN A 204.123.2.18 uucp-gw-2.pa.dec.com 172799 IN A 204.123.2.19To transfer files via UDP, you can use the tftp program.
# tftp 12.34.56.78 tftp> get filename Received XXXXXX bytes in X.X seconds tftp> quit #or, to do it non-interactively (with ugly output):
# echo get filename | tftp 12.34.56.78 tftp> Received XXXXXX bytes in X.X seconds tftp> #This, of course, requires that you have put the file filename in the root directory of the tftp server (12.34.56.78).
It is also possible to run NFS via UDP. This is very useful if you want to share entire directory trees between the emulated environment and another machine. These instruction will work for FreeBSD, if you are running something else, use your imagination to modify them:
/tftpboot -mapall=nobody -ro 123.11.22.33where 123.11.22.33 is the IP address of the machine running the emulator process, as seen from the outside world.
# portmap # nfsd -u <--- u for UDP # mountd -n
# mount -o ro,-r=1024,-w=1024,-U,-3 my.server.com:/tftpboot /mnt or # mount my.server.com:/tftpboot /mntIf you don't supply the read and write sizes, there is a risk that the default values are too large. The emulator currently does not handle fragmentation/defragmentation of outgoing packets, so going above the ethernet frame size (1518) is a very bad idea. Incoming packets (reading from nfs) should work, though, for example during an NFS install.
TCP is implemented to some extend, but should not be considered very stable yet. It is enough to let NetBSD/pmax and OpenBSD/pmax install via ftp, though.
Each device should have the following:
void dev_foo_init(struct cpu *cpu, struct memory *mem, uint64_t baseaddr, int irq_nr) { struct foo_data *d; d = malloc(sizeof(struct foo_data)); if (d == NULL) { fprintf(stderr, "out of memory\n"); exit(1); } memset(d, 0, sizeof(struct foo_data)); d->irq_nr = irq_nr; memory_device_register(mem, "foo", baseaddr, DEV_FOO_LENGTH, dev_foo_access, d); /* This should only be here if the device has a tick function: */ cpu_add_tickfunction(cpu, dev_foo_tick, d, FOO_TICKSHIFT); }
struct foo_data { int irq_nr; /* ... */ }
#define FOO_TICKSHIFT 10 void dev_foo_tick(struct cpu *cpu, void *extra) { struct foo_data *d = (struct foo_data *) extra; if (.....) cpu_interrupt(cpu, d->irq_nr); else cpu_interrupt_ack(cpu, d->irq_nr); }
int dev_foo_access(struct cpu *cpu, struct memory *mem, uint64_t relative_addr, unsigned char *data, size_t len, int writeflag, void *extra) { struct foo_data *d = extra; uint64_t idata = 0, odata = 0; idata = memory_readmax64(cpu, data, len); switch (relative_addr) { /* .... */ } if (writeflag == MEM_READ) memory_writemax64(cpu, data, len, odata); /* Perhaps interrupts need to be asserted or deasserted: */ dev_foo_tick(cpu, extra); /* Return successfully. */ return 1; }
The return value of the access function has until 20040702 been a true/false value; 1 for success, or 0 for device access failure. A device access failure will be seen as a MIPS DBE exception from the CPU.
Right now I'm converting the devices to support arbitrary memory latency values. The return value is now the number of cycles that the read or write access took. A value of 1 means one cycle, a value of 10 means 10 cycles. Negative values are used for device access failures, and the absolute value of the value is then the number of cycles; a value of -5 means that the access failed, and took 5 cycles.
To be compatible with pre-20040702 devices, a return value of 0 is treated by the caller (in src/memory.c) as a value of -1.