https://github.com/akkartik/mu/blob/master/020syscalls.cc
  1 :(before "End Initialize Op Names")
  2 put_new(Name, "cd", "software interrupt (int)");
  3 
  4 :(before "End Single-Byte Opcodes")
  5 case 0xcd: {  // int imm8 (software interrupt)
  6   trace(Callstack_depth+1, "run") << "syscall" << end();
  7   uint8_t code = next();
  8   if (code != 0x80) {
  9     raise << "Unimplemented interrupt code " << HEXBYTE << code << '\n' << end();
 10     raise << "  Only `int 80h` supported for now.\n" << end();
 11     break;
 12   }
 13   process_int80();
 14   break;
 15 }
 16 
 17 :(code)
 18 void process_int80() {
 19   switch (Reg[EAX].u) {
 20   case 1:
 21     exit(/*exit code*/Reg[EBX].u);
 22     break;
 23   case 3:
 24     trace(Callstack_depth+1, "run") << "read: " << Reg[EBX].u << ' ' << Reg[ECX].u << ' ' << Reg[EDX].u << end();
 25     Reg[EAX].i = read(/*file descriptor*/Reg[EBX].u, /*memory buffer*/mem_addr_u8(Reg[ECX].u), /*size*/Reg[EDX].u);
 26     trace(Callstack_depth+1, "run") << "result: " << Reg[EAX].i << end();
 27     if (Reg[EAX].i == -1) raise << "read: " << strerror(errno) << '\n' << end();
 28     break;
 29   case 4:
 30     trace(Callstack_depth+1, "run") << "write: " << Reg[EBX].u << ' ' << Reg[ECX].u << ' ' << Reg[EDX].u << end();
 31     trace(Callstack_depth+1, "run") << Reg[ECX].u << " => " << mem_addr_string(Reg[ECX].u, Reg[EDX].u) << end();
 32     Reg[EAX].i = write(/*file descriptor*/Reg[EBX].u, /*memory buffer*/mem_addr_u8(Reg[ECX].u), /*size*/Reg[EDX].u);
 33     trace(Callstack_depth+| grep -m 1 -o '[0-9][0-9.]\+')
SNAPSHOT_NAME ?= $(NAME)-$(VERSION)-$(shell git rev-parse HEAD | cut -b 1-8).tar.gz
# Find suitable python version (need python >= 2.6 or 3.1):
PYTHON ?= $(shell python -c 'import sys; sys.exit(sys.version < "2.6")' && \
	which python || which python3.1 || which python3 || which python2.6)
SETUPOPTS ?= '--record=install_log.txt'
DOCDIR ?= doc/pydoc
DESTDIR ?= /
PYOPTIMIZE ?= 1
BMCOUNT ?= 5  # how often to run the benchmarks?

CWD = $(shell pwd)

default: compile
	@echo 'Run `make options` for a list of all options'

options: help
	@echo
	@echo 'Options:'
	@echo 'PYTHON = $(PYTHON)'
	@echo 'PYOPTIMIZE = $(PYOPTIMIZE)'
	@echo 'DOCDIR = $(DOCDIR)'

help:
	@echo 'make install: Install $(NAME)'
	@echo 'make doc: Create the pydoc documentation'
	@echo 'make clean: Remove the compiled files (*.pyc, *.pyo)'
	@echo 'make cleandoc: Remove the pydoc documentation'
	@echo 'make snapshot: Create a tar.gz of the current git revision'
	@echo 'make test: Run all unittests.'

install:
	$(PYTHON) setup.py install $(SETUPOPTS) \
		'--root=$(DESTDIR)' --optimize=$(PYOPTIMIZE)

compile: clean
	PYTHONOPTIMIZE=$(PYOPTIMIZE) $(PYTHON) -m compileall -q ranger

clean:
	find . -regex .\*.py[co]\$$ -exec rm -f -- {} \;

doc: cleandoc
	mkdir -p $(DOCDIR)
	cd $(DOCDIR); \
		$(PYTHON) -c 'import pydoc, sys; \
		sys.path[0] = "$(CWD)"; \
		pydoc.writedocs("$(CWD)")'
	rm $(DOCDIR)/test*
	find . -name \*.html -exec sed -i 's|'$(CWD)'|../..|g' -- {} \;

cleandoc:
	test -d $(DOCDIR) && rm -f -- $(DOCDIR)/*.html || true

test:
	@$(PYTHON) test/all_tests.py 1

bm:
	@$(PYTHON) test/all_benchmarks.py $(BMCOUNT)

snapshot:
	git archive --prefix='$(NAME)-$(VERSION)/' --format=tar HEAD | gzip > $(SNAPSHOT_NAME)

.PHONY: default options compile clean doc cleandoc test bm snapshot install
creat(/*filename*/mem_addr_kernel_string(Reg[EBX].u), /*mode*/0640); 57 trace(Callstack_depth+1, "run") << "result: " << Reg[EAX].i << end(); 58 if (Reg[EAX].i == -1) raise << "creat: " << strerror(errno) << '\n' << end(); 59 break; 60 case 10: 61 trace(Callstack_depth+1, "run") << "unlink: " << Reg[EBX].u << end(); 62 trace(Callstack_depth+1, "run") << Reg[EBX].u << " => " << mem_addr_kernel_string(Reg[EBX].u) << end(); 63 Reg[EAX].i = unlink(/*filename*/mem_addr_kernel_string(Reg[EBX].u)); 64 trace(Callstack_depth+1, "run") << "result: " << Reg[EAX].i << end(); 65 if (Reg[EAX].i == -1) raise << "unlink: " << strerror(errno) << '\n' << end(); 66 break; 67 case 38: 68 trace(Callstack_depth+1, "run") << "rename: " << Reg[EBX].u << " -> " << Reg[ECX].u << end(); 69 trace(Callstack_depth+1, "run") << Reg[EBX].u << " => " << mem_addr_kernel_string(Reg[EBX].u) << end(); 70 trace(Callstack_depth+1, "run") << Reg[ECX].u << " => " << mem_addr_kernel_string(Reg[ECX].u) << end(); 71 Reg[EAX].i = rename(/*old filename*/mem_addr_kernel_string(Reg[EBX].u), /*new filename*/mem_addr_kernel_string(Reg[ECX].u)); 72 trace(Callstack_depth+1, "run") << "result: " << Reg[EAX].i << end(); 73 if (Reg[EAX].i == -1) raise << "rename: " << strerror(errno) << '\n' << end(); 74 break; 75 case 90: // mmap: allocate memory outside existing segment allocations 76 trace(Callstack_depth+1, "run") << "mmap: allocate new segment" << end(); 77 // Ignore most arguments for now: address hint, protection flags, sharing flags, fd, offset. 78 // We only support anonymous maps. 79 Reg[EAX].u = new_segment(/*length*/read_mem_u32(Reg[EBX].u+0x4)); 80 trace(Callstack_depth+1, "run") << "result: " << Reg[EAX].u << end(); 81 break; 82 case 0xa2: // nanosleep 83 cerr << "not sleeping\n"; 84 break; 85 default: 86 raise << HEXWORD << EIP << ": unimplemented syscall " << Reg[EAX].u << '\n' << end(); 87 } 88 } 89 90 // SubX is oblivious to file permissions, directories, symbolic links, terminals, and much else besides. 91 // Also ignoring any concurrency considerations for now. 92 void check_flags(int reg) { 93 uint32_t flags = Reg[reg].u; 94 if (flags != ((flags & O_RDONLY) | (flags & O_WRONLY))) { 95 cerr << HEXWORD << EIP << ": most POSIX flags to the open() syscall are not supported. Just O_RDONLY and O_WRONLY for now. Zero concurrent access support.\n"; 96 exit(1); 97 } 98 if ((flags & O_RDONLY) && (flags & O_WRONLY)) { 99 cerr << HEXWORD << EIP << ": can't open a file for both reading and writing at once. See http://man7.org/linux/man-pages/man2/open.2.html.\n"; 100 exit(1); 101 } 102 } 103 104 void check_mode(int reg) { 105 if (Reg[reg].u != 0600) { 106 cerr << HEXWORD << EIP << ": SubX is oblivious to file permissions; register " << reg << " must be 0x180.\n"; 107 exit(1); 108 } 109 } 110 111 :(before "End Globals") 112 // Very primitive/fixed/insecure mmap segments for now. 113 uint32_t Segments_allocated_above = END_HEAP; 114 :(code) 115 // always allocate multiples of the segment size 116 uint32_t new_segment(uint32_t length) { 117 assert(length > 0); 118 uint32_t result = (Segments_allocated_above - length) & 0xff000000; // same number of zeroes as SEGMENT_ALIGNMENT 119 if (result <= START_HEAP) { 120 raise << "Allocated too many segments; the VM ran out of memory. " 121 << "Maybe SEGMENT_ALIGNMENT can be smaller?\n" << die(); 122 } 123 Mem.push_back(vma(result, result+length)); 124 Segments_allocated_above = result; 125 return result; 126 }