From 7328af20a1921d9258a60803ee5367da97a6082e Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Mon, 13 Aug 2018 21:25:22 -0700 Subject: 4521 --- html/subx/020syscalls.cc.html | 143 ++++++++++++++++++++++++++++++++---------- 1 file changed, 111 insertions(+), 32 deletions(-) (limited to 'html/subx/020syscalls.cc.html') diff --git a/html/subx/020syscalls.cc.html b/html/subx/020syscalls.cc.html index c29ac354..c4d39b2e 100644 --- a/html/subx/020syscalls.cc.html +++ b/html/subx/020syscalls.cc.html @@ -22,6 +22,7 @@ a:hover { text-decoration: underline; } .LineNr { color: #444444; } .Identifier { color: #c0a020; } .Normal { color: #aaaaaa; background-color: #080808; padding-bottom: 1px; } +.PreProc { color: #800080; } .cSpecial { color: #008000; } --> @@ -57,38 +58,116 @@ if ('onhashchange' in window) {
- 1 :(before "End Initialize Op Names(name)")
- 2 put(name, "cd", "software interrupt");
- 3 
- 4 :(before "End Single-Byte Opcodes")
- 5 case 0xcd: {  // int imm8 (software interrupt)
- 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(/*exit code*/Reg[EBX].u);
-22     break;
-23   case 3:
-24     Reg[EAX].i = read(/*file descriptor*/Reg[EBX].u, /*memory buffer*/mem_addr_u8(Reg[ECX].u), /*size*/Reg[EDX].u);
-25     break;
-26   case 4:
-27     Reg[EAX].i = write(/*file descriptor*/Reg[EBX].u, /*memory buffer*/mem_addr_u8(Reg[ECX].u), /*size*/Reg[EDX].u);
-28     break;
-29   default:
-30     raise << HEXWORD << EIP << ": unimplemented syscall " << Reg[EAX].u << '\n' << end();
-31   }
-32 }
+  1 :(before "End Initialize Op Names(name)")
+  2 put(name, "cd", "software interrupt");
+  3 
+  4 :(before "End Single-Byte Opcodes")
+  5 case 0xcd: {  // int imm8 (software interrupt)
+  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(/*exit code*/Reg[EBX].u);
+ 22     break;
+ 23   case 3:
+ 24     trace(91, "run") << "read: " << Reg[EBX].u << ' ' << Reg[ECX].u << '/' << mem_addr_string(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(91, "run") << "result: " << Reg[EAX].i << end();
+ 27     if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
+ 28     break;
+ 29   case 4:
+ 30     trace(91, "run") << "write: " << Reg[EBX].u << ' ' << Reg[ECX].u << '/' << mem_addr_string(Reg[ECX].u) << ' ' << Reg[EDX].u << end();
+ 31     Reg[EAX].i = write(/*file descriptor*/Reg[EBX].u, /*memory buffer*/mem_addr_u8(Reg[ECX].u), /*size*/Reg[EDX].u);
+ 32     trace(91, "run") << "result: " << Reg[EAX].i << end();
+ 33     if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
+ 34     break;
+ 35   case 5: {
+ 36     check_flags(ECX);
+ 37     check_mode(EDX);
+ 38     trace(91, "run") << "open: " << Reg[EBX].u << '/' << mem_addr_string(Reg[EBX].u) << ' ' << Reg[ECX].u << end();
+ 39     Reg[EAX].i = open(/*filename*/mem_addr_string(Reg[EBX].u), /*flags*/Reg[ECX].u, /*mode*/0640);
+ 40     trace(91, "run") << "result: " << Reg[EAX].i << end();
+ 41     if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
+ 42     break;
+ 43   }
+ 44   case 6:
+ 45     trace(91, "run") << "close: " << Reg[EBX].u << end();
+ 46     Reg[EAX].i = close(/*file descriptor*/Reg[EBX].u);
+ 47     trace(91, "run") << "result: " << Reg[EAX].i << end();
+ 48     if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
+ 49     break;
+ 50   case 8:
+ 51     check_mode(ECX);
+ 52     trace(91, "run") << "creat: " << Reg[EBX].u << '/' << mem_addr_string(Reg[EBX].u) << end();
+ 53     Reg[EAX].i = creat(/*filename*/mem_addr_string(Reg[EBX].u), /*mode*/0640);
+ 54     trace(91, "run") << "result: " << Reg[EAX].i << end();
+ 55     if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
+ 56     break;
+ 57   case 10:
+ 58     trace(91, "run") << "unlink: " << Reg[EBX].u << '/' << mem_addr_string(Reg[EBX].u) << end();
+ 59     Reg[EAX].i = unlink(/*filename*/mem_addr_string(Reg[EBX].u));
+ 60     trace(91, "run") << "result: " << Reg[EAX].i << end();
+ 61     if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
+ 62     break;
+ 63   case 38:
+ 64     trace(91, "run") << "rename: " << Reg[EBX].u << '/' << mem_addr_string(Reg[EBX].u) << " -> " << Reg[ECX].u << '/' << mem_addr_string(Reg[ECX].u) << end();
+ 65     Reg[EAX].i = rename(/*old filename*/mem_addr_string(Reg[EBX].u), /*new filename*/mem_addr_string(Reg[ECX].u));
+ 66     trace(91, "run") << "result: " << Reg[EAX].i << end();
+ 67     if (Reg[EAX].i == -1) raise << strerror(errno) << '\n' << end();
+ 68     break;
+ 69   case 45:  // brk: modify size of data segment
+ 70     trace(91, "run") << "grow data segment to " << Reg[EBX].u << end();
+ 71     resize_mem(/*new end address*/Reg[EBX].u);
+ 72     break;
+ 73   default:
+ 74     raise << HEXWORD << EIP << ": unimplemented syscall " << Reg[EAX].u << '\n' << end();
+ 75   }
+ 76 }
+ 77 
+ 78 // SubX is oblivious to file permissions, directories, symbolic links, terminals, and much else besides.
+ 79 // Also ignoring any concurrency considerations for now.
+ 80 void check_flags(int reg) {
+ 81   uint32_t flags = Reg[reg].u;
+ 82   if (flags != ((flags & O_RDONLY) | (flags & O_WRONLY))) {
+ 83     raise << 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" << end();
+ 84     exit(1);
+ 85   }
+ 86   if ((flags & O_RDONLY) && (flags & O_WRONLY)) {
+ 87     raise << 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" << end();
+ 88     exit(1);
+ 89   }
+ 90 }
+ 91 
+ 92 void check_mode(int reg) {
+ 93   if (Reg[reg].u != 0600) {
+ 94     raise << HEXWORD << EIP << ": SubX is oblivious to file permissions; register " << reg << " must be 0.\n" << end();
+ 95     exit(1);
+ 96   }
+ 97 }
+ 98 
+ 99 void resize_mem(uint32_t new_end_address) {
+100   if (new_end_address < Mem_offset) {
+101     raise << HEXWORD << EIP << ": can't shrink data segment to before code segment\n";
+102     return;
+103   }
+104   int32_t new_size = new_end_address - Mem_offset;
+105   if (new_size < SIZE(Mem)) {
+106     raise << HEXWORD << EIP << ": shrinking data segment is not supported.\n" << end();
+107     return;
+108   }
+109   Mem.resize(new_size);  // will throw exception on failure
+110 }
 
-- cgit 1.4.1-2-gfad0