-
+ CA9D23D0ADA3927D70455EC847E8C92ECF88E4B8A4295F07D89155D8322F7A096C47895C506C451F9D1F12F58BC4FBEA4D01D76E673755D76A93ED81206554D4
m/m.asm
(0 . 0)(1 . 156)
1632 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1633 ;; 'M', a MIPS system emulator. ;;
1634 ;; Version: 300K. ;;
1635 ;; ;;
1636 ;; Will run lightly-modified Linux kernels with full isolation vs. host. ;;
1637 ;; To keep things simple, dynamic recompilation a la Bellard's is NOT USED! ;;
1638 ;; ;;
1639 ;; 'M' aims to 'fit-in-head'. As such, a reasonably complete description ;;
1640 ;; of the emulated machine architecture is included in the comments. ;;
1641 ;; ;;
1642 ;; Dependencies/Libraries required : NONE!!! ;;
1643 ;; Where Runs: Any AMD64 linux. ;;
1644 ;; ;;
1645 ;; To build: ;;
1646 ;; 'make' (needs 'gnumake') ;;
1647 ;; or: ;;
1648 ;; (1) yasm -f elf64 -g null m.asm ;;
1649 ;; (2) ld m.o -o m ;;
1650 ;; (3) strip m ;;
1651 ;; At the time of writing, yields a <13kB ELF. ;;
1652 ;; ;;
1653 ;; To run: ;;
1654 ;; ./bin/m kernel.bin ;;
1655 ;; ;;
1656 ;; Note: currently the only means to exit (other than 'kill -9') is to ;;
1657 ;; shut down the guest OS (In 'busybox' -- 'poweroff' command.) ;;
1658 ;; ;;
1659 ;; Devices Currently Emulated (see 'devices' dir) : ;;
1660 ;; MIPS Timer, 100Hz Timer, UART Console, Realtime Clock, Power Switch. ;;
1661 ;; ;;
1662 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1663 ;; ;;
1664 ;; (C) 2019 Stanislav Datskovskiy ( www.loper-os.org ) ;;
1665 ;; http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html ;;
1666 ;; ;;
1667 ;; You do not have, nor can you ever acquire the right to use, copy or ;;
1668 ;; distribute this software ; Should you use this software for any purpose, ;;
1669 ;; or copy and distribute it to anyone or in any manner, you are breaking ;;
1670 ;; the laws of whatever soi-disant jurisdiction, and you promise to ;;
1671 ;; continue doing so for the indefinite future. In any case, please ;;
1672 ;; always : read and understand any software ; verify any PGP signatures ;;
1673 ;; that you use - for any purpose. ;;
1674 ;; ;;
1675 ;; See also http://trilema.com/2015/a-new-software-licensing-paradigm . ;;
1676 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1677
1678 ;-----------------------------------------------------------------------------
1679 %include "knobs.asm" ; User-adjustable Knobs
1680 %include "log.asm" ; Eggogology and Warning reporting
1681 %include "os/linux.asm" ; Linux invariants and threading knob
1682 %include "os/linux_io.asm" ; Linux I/O routines
1683 %include "flags.asm" ; Emulator State Flags
1684 %include "mips.asm" ; MIPS CPU Invariants
1685 %include "i_decode.asm" ; MIPS Instruction Decoding
1686 %include "cpustate.asm" ; MIPS CPU State
1687 %include "mips_exc.asm" ; MIPS Exception Handler
1688 %include "irq.asm" ; MIPS Interrupts
1689 %include "ram.asm" ; Memory
1690 %include "bus.asm" ; Memory-Mapped Devices
1691 %include "mips_cpu.asm" ; MIPS CPU Cycle Execution
1692 %include "mipsinst/i_instrs.asm" ; I-Type MIPS Instructions
1693 %include "mipsinst/r_instrs.asm" ; R-Type MIPS Instructions
1694 %include "mipsinst/b_instrs.asm" ; B-Type MIPS Instructions
1695 %include "mipsinst/m_instrs.asm" ; M-Type MIPS Instructions
1696 %include "shutdown.asm" ; Termination Cleanup
1697 ;-----------------------------------------------------------------------------
1698
1699 ;-----------------------------------------------------------------------------
1700 ;; State
1701 ;-----------------------------------------------------------------------------
1702 section .bss
1703 arg0 resb 8 ; First Command Line Argument
1704 fd resb 8 ; FD of file containing kernel image
1705 ;-----------------------------------------------------------------------------
1706
1707 ;; TODO: 1) Make RAM size adjustable. 2) Eat kernels in ELF form ?
1708 ;; 3) Move ALL os-istic calls into os/linux.asm ?
1709 ;; 4) Right now we use 100% of host CPU. Need sleep in wait and futex.
1710 ;; 5) Need devices! particularly 'disk' and 'NIC'. (And FG...!)
1711 ;; 6) Bring UART0 console out as TCP-able?
1712 ;; 7) 'Suspend/Resume', out-of-band debuggisms?
1713 ;; 8) Tests!!! Particularly, per-instruction test cases! Can you write?
1714
1715 ;-----------------------------------------------------------------------------
1716 ; Start of Program.
1717 ;-----------------------------------------------------------------------------
1718 section .text
1719 global _start
1720 _start:
1721 ;; Get argc (# of command line arguments) :
1722 mov rax, [rsp]
1723 cmp rax, CMDLINE_ARG_COUNT + 1 ; The required arg. count
1724 je ._run
1725 ;; Not correct number of args? then print usage and exit:
1726 ._usage:
1727 EGGOG "Usage: ./M KERNEL"
1728 ._run:
1729 ;; Test if SSE2 instructions are available on this machine:
1730 mov eax, 1
1731 cpuid
1732 test edx, 0x4000000
1733 jnz ._xmm_ok
1734 EGGOG "Needs SSE2!"
1735 ._xmm_ok:
1736
1737 ;; Get 1st cmdline arg (path)
1738 mov rdi, [rsp + 16]
1739 mov [arg0], rdi
1740
1741 ;; fd = open(path, O_RDONLY)
1742 mov rax, SYS_OPEN
1743 mov rdi, [arg0] ; first arg
1744 mov rsi, 0 ; O_RDONLY
1745 syscall
1746 test rax, rax ; see if eggog
1747 jns ._ok ; if worked
1748 EGGOG "Could not read kernel from disk!"
1749 ._ok:
1750 mov [fd], rax ; else, save fd
1751
1752 ;; Mbytes = fstat(fd).st_size (footprint of initial image)
1753 mov rax, SYS_FSTAT
1754 sub rsp, statbuf.size ; make scratch
1755 mov rsi, rsp
1756 mov rdi, [fd]
1757 syscall
1758 mov rax, [rsp + statbuf.st_size]
1759 mov [Mbytes], rax
1760 add rsi, statbuf.size ; unmake scratch
1761
1762 ;; RAM size
1763 mov qword [RAMbytes], RAM_SIZE_MB * (1024 * 1024)
1764 call _ram_allocate
1765 mov [M], rax
1766
1767 ;; Load the given kernel into bottom of sim RAM:
1768 mov rax, SYS_READ
1769 mov rdi, [fd] ; fd of memory snapshot
1770 mov rsi, [M] ; where to put
1771 mov rdx, [Mbytes] ; read whole snapshot into bottom of sim ram
1772 syscall
1773
1774 ;; close(fd)
1775 mov rax, SYS_CLOSE
1776 mov rdi, [fd]
1777 syscall
1778
1779 ;; Initialize all MMIO Devices (and start all slave threads) :
1780 call _Phys_Devices_Initialize
1781
1782 ;-----------------------------------------------------------------------------
1783 _Master_Thread:
1784 call _ram_init ; Initialize RAM
1785 call _cpu_reset ; Reset the MIPS CPU
1786 jmp _cycle ; Start the engine
1787 ;-----------------------------------------------------------------------------