1 :(before "End Initialize Op Names(name)")
2 put(name, "cd", "software interrupt");
3
4 :(before "End Single-Byte Opcodes")
5 case 0xcd: {
6 trace(90, "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(Reg[EBX].u);
22 break;
23 case 3:
24 trace(91, "run") << "read: " << Reg[EBX].u << ' ' << Reg[ECX].u << ' ' << Reg[EDX].u << end();
25 trace(91, "run") << Reg[ECX].u << " => " << mem_addr_string(Reg[ECX].u) << end();
26 Reg[EAX].i = read(Reg[EBX].u, mem_addr_u8(Reg[ECX].u), Reg[EDX].u);
27 trace(91, "run") << "result: " << Reg[EAX].i << end();
28 if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
29 break;
30 case 4:
31 trace(91, "run") << "write: " << Reg[EBX].u << ' ' << Reg[ECX].u << ' ' << Reg[EDX].u << end();
32 trace(91, "run") << Reg[ECX].u << " => " << mem_addr_string(Reg[ECX].u) << end();
33 Reg[EAX].i = write(Reg[EBX].u, mem_addr_u8(Reg[ECX].u), Reg[EDX].u);
34 trace(91, "run") << "result: " << Reg[EAX].i << end();
35 if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
36 break;
37 case 5: {
38 check_flags(ECX);
39 check_mode(EDX);
40 trace(91, "run") << "open: " << Reg[EBX].u << ' ' << Reg[ECX].u << end();
41 trace(91, "run") << Reg[EBX].u << " => " << mem_addr_string(Reg[EBX].u) << end();
42 Reg[EAX].i = open(mem_addr_string(Reg[EBX].u), Reg[ECX].u, 0640);
43 trace(91, "run") << "result: " << Reg[EAX].i << end();
44 if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
45 break;
46 }
47 case 6:
48 trace(91, "run") << "close: " << Reg[EBX].u << end();
49 Reg[EAX].i = close(Reg[EBX].u);
50 trace(91, "run") << "result: " << Reg[EAX].i << end();
51 if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
52 break;
53 case 8:
54 check_mode(ECX);
55 trace(91, "run") << "creat: " << Reg[EBX].u << end();
56 trace(91, "run") << Reg[EBX].u << " => " << mem_addr_string(Reg[EBX].u) << end();
57 Reg[EAX].i = creat(mem_addr_string(Reg[EBX].u), 0640);
58 trace(91, "run") << "result: " << Reg[EAX].i << end();
59 if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
60 break;
61 case 10:
62 trace(91, "run") << "unlink: " << Reg[EBX].u << end();
63 trace(91, "run") << Reg[EBX].u << " => " << mem_addr_string(Reg[EBX].u) << end();
64 Reg[EAX].i = unlink(mem_addr_string(Reg[EBX].u));
65 trace(91, "run") << "result: " << Reg[EAX].i << end();
66 if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
67 break;
68 case 38:
69 trace(91, "run") << "rename: " << Reg[EBX].u << " -> " << Reg[ECX].u << end();
70 trace(91, "run") << Reg[EBX].u << " => " << mem_addr_string(Reg[EBX].u) << end();
71 trace(91, "run") << Reg[ECX].u << " => " << mem_addr_string(Reg[ECX].u) << end();
72 Reg[EAX].i = rename(mem_addr_string(Reg[EBX].u), mem_addr_string(Reg[ECX].u));
73 trace(91, "run") << "result: " << Reg[EAX].i << end();
74 if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
75 break;
76 case 45:
77 trace(91, "run") << "grow data segment to " << Reg[EBX].u << end();
78 resize_mem(Reg[EBX].u);
79 break;
80 default:
81 raise << HEXWORD << EIP << ": unimplemented syscall " << Reg[EAX].u << '\n' << end();
82 }
83 }
84
85
86
87 void check_flags(int reg) {
88 uint32_t flags = Reg[reg].u;
89 if (flags != ((flags & O_RDONLY) | (flags & O_WRONLY))) {
90 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";
91 exit(1);
92 }
93 if ((flags & O_RDONLY) && (flags & O_WRONLY)) {
94 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";
95 exit(1);
96 }
97 }
98
99 void check_mode(int reg) {
100 if (Reg[reg].u != 0600) {
101 cerr << HEXWORD << EIP << ": SubX is oblivious to file permissions; register " << reg << " must be 0.\n";
102 exit(1);
103 }
104 }
105
106 void resize_mem(uint32_t new_end_address) {
107 if (new_end_address < Mem_offset) {
108 raise << HEXWORD << EIP << ": can't shrink data segment to before code segment\n";
109 return;
110 }
111 int32_t new_size = new_end_address - Mem_offset;
112 if (new_size < SIZE(Mem)) {
113 raise << HEXWORD << EIP << ": shrinking data segment is not supported.\n" << end();
114 return;
115 }
116 Mem.resize(new_size);
117 }