{"id":1394,"date":"2012-03-03T17:56:39","date_gmt":"2012-03-02T23:56:39","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=1394"},"modified":"2016-03-03T17:49:42","modified_gmt":"2016-03-03T08:49:42","slug":"xv6%e3%82%bd%e3%83%bc%e3%82%b9%e3%82%b3%e3%83%bc%e3%83%89%e3%83%aa%e3%83%bc%e3%83%87%e3%82%a3%e3%83%b3%e3%82%b0-%e3%81%9d%e3%81%ae27","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/1394","title":{"rendered":"[xv6 #27] Chapter 2 &#8211; Traps, interrupts, and drivers &#8211; Code: System calls"},"content":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e36\u301c37\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5834\u5408\u3001trap\u95a2\u6570\u306fsyscall\u95a2\u6570\u3092\u547c\u3076\u3002<br \/>\nsyscall\u95a2\u6570\u306f\u3001\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u304b\u3089\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u756a\u53f7\u3092\u8aad\u307f\u8fbc\u3080\u3002<br \/>\n\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u306f\u4fdd\u5b58\u3055\u308c\u305f%eax\u3092\u542b\u307f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30c6\u30fc\u30d6\u30eb\u306e\u7279\u5b9a\u306e\u30a8\u30f3\u30c8\u30ea\u3092\u6307\u3059\u3002<br \/>\n\u6700\u521d\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5834\u5408\u3001%eax\u306fSYS_exec\u3068\u3044\u3046\u5024\u3092\u542b\u3093\u3067\u3044\u308b\u3002\uff08#define SYS_exec 7\uff09<br \/>\n\u305d\u3057\u3066syscall\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30c6\u30fc\u30d6\u30eb\u306eSYS_exec\u756a\u76ee\u306e\u30a8\u30f3\u30c8\u30ea\u3092\u547c\u3073\u51fa\u3059\u3002<br \/>\n\u305d\u306e\u30a8\u30f3\u30c8\u30ea\u306f\u3001sys_exec\u95a2\u6570\u306e\u547c\u3073\u51fa\u3057\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u3002<\/p>\n<p>syscall.c\u306esyscall\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">void\r\nsyscall(void)\r\n{\r\n  int num;\r\n\r\n  num = proc-&gt;tf-&gt;eax;\r\n  if(num &gt;= 0 &amp;&amp; num &lt; SYS_open &amp;&amp; syscalls&#x5B;num]) {\r\n    proc-&gt;tf-&gt;eax = syscalls&#x5B;num]();\r\n  } else if (num &gt;= SYS_open &amp;&amp; num &lt; NELEM(syscalls) &amp;&amp; syscalls&#x5B;num]) {\r\n    proc-&gt;tf-&gt;eax = syscalls&#x5B;num]();\r\n  } else {\r\n    cprintf(&quot;%d %s: unknown sys call %d\\n&quot;,\r\n            proc-&gt;pid, proc-&gt;name, num);\r\n    proc-&gt;tf-&gt;eax = -1;\r\n  }\r\n}<\/pre>\n<p>syscall\u95a2\u6570\u306f\u3001%eax\u304c\u6307\u3059\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u95a2\u6570\u306e\u623b\u308a\u5024\u3092\u8a18\u9332\u3059\u308b\u3002<br \/>\n\u30c8\u30e9\u30c3\u30d7\u304b\u3089\u30e6\u30fc\u30b6\u30b9\u30da\u30fc\u30b9\u306b\u623b\u3063\u305f\u3068\u304d\u3001\u305d\u308c\u306fcp-&gt;tf\u304b\u3089\u30de\u30b7\u30f3\u306e\u30ec\u30b8\u30b9\u30bf\u306b\u30ed\u30fc\u30c9\u3055\u308c\u308b\u3060\u308d\u3046\u3002<br \/>\n\u3053\u306e\u3088\u3046\u306b\u3001exec\u304b\u3089\u623b\u3063\u305f\u3068\u304d\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30cf\u30f3\u30c9\u30e9\u304b\u3089\u8fd4\u3063\u305f\u5024\u3092\u8fd4\u3059\u3060\u308d\u3046\u3002\uff08syscall\u95a2\u6570\u4e2d\u306e\u6700\u521d\u306eproc\u2212&gt;tf\u2212&gt;eax = syscalls&#091;num&#093;();\u306e\u90e8\u5206\uff09<br \/>\n\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306f\u3001\u6163\u7fd2\u7684\u306b\u30a8\u30e9\u30fc\u3092\u901a\u77e5\u3059\u308b\u305f\u3081\u306b\u8ca0\u306e\u5024\u3092\u8fd4\u3057\u3001\u6210\u529f\u3057\u305f\u3068\u304d\u306f\u6b63\u306e\u5024\u3092\u8fd4\u3059\u3002<br \/>\n\u3082\u3057\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u756a\u53f7\u304c\u7570\u5e38\u306a\u3089\u3001syscall\u95a2\u6570\u306f\u30a8\u30e9\u30fc\u3092\u5370\u5b57\u3057-1\u3092\u8fd4\u3059\u3002<\/p>\n<p>\u5f8c\u306e\u7ae0\u3067\u3001\u500b\u5225\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5b9f\u88c5\u306b\u3064\u3044\u3066\u8aac\u660e\u3059\u308b\u3002<br \/>\n\u3053\u306e\u7ae0\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u30e1\u30ab\u30cb\u30ba\u30e0\u306b\u95a2\u4fc2\u3057\u3066\u3044\u308b\u3002<br \/>\n\u30e1\u30ab\u30cb\u30ba\u30e0\u304c\u5f8c\u4e00\u3064\u6b8b\u3063\u3066\u3044\u308b\u3002<br \/>\n\u305d\u308c\u306f\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5f15\u6570\u3092\u898b\u3064\u3051\u308b\u4ed5\u7d44\u307f\u306b\u3064\u3044\u3066\u3067\u3042\u308b\u3002<br \/>\nargint\u3068argptr\u3068argstr\u3068\u3044\u3046\u30d8\u30eb\u30d1\u95a2\u6570\u306f\u3001n\u756a\u76ee\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5f15\u6570\u3092\u53d6\u308a\u51fa\u3059\u3002<br \/>\n\u305d\u308c\u305e\u308c\u9806\u756a\u306b\u3001\u6574\u6570\u3001\u30dd\u30a4\u30f3\u30bf\u3001\u6587\u5b57\u5217\u306b\u5bfe\u5fdc\u3059\u308b\u3002<br \/>\nargint\u306f\u3001n\u756a\u76ee\u306e\u5f15\u6570\u3092\u7279\u5b9a\u3059\u308b\u305f\u3081\u306b\u3001\u30e6\u30fc\u30b6\u7a7a\u9593\u306e%esp\u30ec\u30b8\u30b9\u30bf\u3092\u4f7f\u3046\u3002<br \/>\n%esp\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u30b9\u30bf\u30d6\u306e\u305f\u3081\u306e\u623b\u308a\u5148\u30a2\u30c9\u30ec\u30b9\u3092\u6307\u3057\u3066\u3044\u308b\u3002<br \/>\n\u5f15\u6570\u306f\u3001\u307e\u3055\u306b\u305d\u306e\uff08\u623b\u308a\u5148\u30a2\u30c9\u30ec\u30b9\uff09\u306e\u3059\u3050\u4e0a\u4f4d\u306b\u3042\u308a\u3001\u305d\u308c\u306f%esp+4\u3067\u5c0e\u304d\u51fa\u305b\u308b\u3002<br \/>\n\u3060\u304b\u3089n\u756a\u76ee\u306e\u5f15\u6570\u306f\u3001%esp+4+4*n\u3067\u5c0e\u304d\u51fa\u305b\u308b\u3002<\/p>\n<p>syscall.c\u306eargint, argptr, argstr\u95a2\u9023\u306e\u90e8\u5206<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ User code makes a system call with INT T_SYSCALL.\r\n\/\/ System call number in %eax.\r\n\/\/ Arguments on the stack, from the user call to the C\r\n\/\/ library system call function. The saved user %esp points\r\n\/\/ to a saved program counter, and then the first argument.\r\n\r\n\/\/ Fetch the int at addr from process p.\r\nint\r\nfetchint(struct proc *p, uint addr, int *ip)\r\n{\r\n  if(addr &gt;= p-&gt;sz || addr+4 &gt; p-&gt;sz)\r\n    return -1;\r\n  *ip = *(int*)(addr);\r\n  return 0;\r\n}\r\n\r\n\/\/ Fetch the nul-terminated string at addr from process p.\r\n\/\/ Doesn't actually copy the string - just sets *pp to point at it.\r\n\/\/ Returns length of string, not including nul.\r\nint\r\nfetchstr(struct proc *p, uint addr, char **pp)\r\n{\r\n  char *s, *ep;\r\n\r\n  if(addr &gt;= p-&gt;sz)\r\n    return -1;\r\n  *pp = (char*)addr;\r\n  ep = (char*)p-&gt;sz;\r\n  for(s = *pp; s &lt; ep; s++)\r\n    if(*s == 0)\r\n      return s - *pp;\r\n  return -1;\r\n}\r\n\r\n\/\/ Fetch the nth 32-bit system call argument.\r\nint\r\nargint(int n, int *ip)\r\n{\r\n  return fetchint(proc, proc-&gt;tf-&gt;esp + 4 + 4*n, ip);\r\n}\r\n\r\n\/\/ Fetch the nth word-sized system call argument as a pointer\r\n\/\/ to a block of memory of size n bytes.  Check that the pointer\r\n\/\/ lies within the process address space.\r\nint\r\nargptr(int n, char **pp, int size)\r\n{\r\n  int i;\r\n  \r\n  if(argint(n, &amp;i) &lt; 0)\r\n    return -1;\r\n  if((uint)i &gt;= proc-&gt;sz || (uint)i+size &gt; proc-&gt;sz)\r\n    return -1;\r\n  *pp = (char*)i;\r\n  return 0;\r\n}\r\n\r\n\/\/ Fetch the nth word-sized system call argument as a string pointer.\r\n\/\/ Check that the pointer is valid and the string is nul-terminated.\r\n\/\/ (There is no shared writable memory, so the string can't change\r\n\/\/ between this check and being used by the kernel.)\r\nint\r\nargstr(int n, char **pp)\r\n{\r\n  int addr;\r\n  if(argint(n, &amp;addr) &lt; 0)\r\n    return -1;\r\n  return fetchstr(proc, addr, pp);\r\n}<\/pre>\n<p>argint\u95a2\u6570\u306f\u3001\u30e6\u30fc\u30b6\u30e1\u30e2\u30ea\u306b\u3042\u308b\u30a2\u30c9\u30ec\u30b9\u304b\u3089\u5024\u3092\u8aad\u307f\u8fbc\u307f\u3001*ip\u306b\u305d\u308c\u3092\u66f8\u304d\u8fbc\u3080\u305f\u3081\u306bfetchint\u95a2\u6570\u3092\u547c\u3076\u3002<br \/>\nfetchint\u306f\u3001\u305d\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u7c21\u5358\u306b\u30dd\u30a4\u30f3\u30bf\u306b\u30ad\u30e3\u30b9\u30c8\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br \/>\n\u306a\u305c\u306a\u3089\u3001\u30e6\u30fc\u30b6\u3068\u30ab\u30fc\u30cd\u30eb\u306f\u540c\u3058\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u3092\u5171\u6709\u3057\u3066\u3044\u308b\u304b\u3089\u3067\u3042\u308b\u3002<br \/>\n\u3057\u304b\u3057\u3001\u30ab\u30fc\u30cd\u30eb\u306f\u3001\u30e6\u30fc\u30b6\u306b\u3088\u308b\u305d\u306e\u30dd\u30a4\u30f3\u30bf\u304c\u30a2\u30c9\u30ec\u30b9\u7a7a\u9593\u306e\u30e6\u30fc\u30b6\u306e\u90e8\u5206\u306b\u3042\u308b\u30dd\u30a4\u30f3\u30bf\u3067\u3042\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\n\u30ab\u30fc\u30cd\u30eb\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u304c\u305d\u308c\u81ea\u8eab\u306e\u30ed\u30fc\u30ab\u30eb\u3067\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u306a\u30e1\u30e2\u30ea\u306e\u5916\u306b\u30a2\u30af\u30bb\u30b9\u51fa\u6765\u306a\u3044\u3053\u3068\u3092\u78ba\u5b9f\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u305f\u3002<br \/>\n\u3082\u3057\u3001\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u304cp-&gt;sz\u3088\u308a\u4e0a\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u8aad\u307f\u66f8\u304d\u3057\u3088\u3046\u3068\u3057\u305f\u3089\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u30bb\u30b0\u30e1\u30f3\u30c6\u30fc\u30b7\u30e7\u30f3\u30c8\u30e9\u30c3\u30d7\u3092\u5f15\u304d\u8d77\u3053\u3057\u3001\u305d\u3057\u3066\u305d\u306e\u30c8\u30e9\u30c3\u30d7\u306f\u30d7\u30ed\u30bb\u30b9\u3092\u6bba\u3059\u3002<br \/>\n\u6211\u3005\u304c\u3044\u307e\u307e\u3067\u898b\u3066\u304d\u305f\u3088\u3046\u306b\u3002<br \/>\n\u4eca\u3001\u30ab\u30fc\u30cd\u30eb\u306f\u5b9f\u884c\u4e2d\u3067\u3042\u308a\u3001\u30e6\u30fc\u30b6\u304c\u6e21\u3057\u305f\u304b\u3082\u3057\u308c\u306a\u3044\u3069\u306e\u30a2\u30c9\u30ec\u30b9\u3082\u9006\u53c2\u7167\u53ef\u80fd\u3067\u3042\u308b\u3001\u3068\u306f\u3044\u3048\u3001\u305d\u306e\u30a2\u30c9\u30ec\u30b9\u304cp-&gt;sz\u672a\u6e80\u3067\u3042\u308b\u3053\u3068\u3092\u30ab\u30fc\u30cd\u30eb\u306f\u78ba\u8a8d\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/p>\n<p>argptr\u95a2\u6570\u306f\u3001argint\u306e\u7528\u9014\u3068\u4f3c\u3066\u3044\u308b\u3002<br \/>\nargptr\u95a2\u6570\u306f\u3001n\u756a\u76ee\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5f15\u6570\u3092\u89e3\u91c8\u3059\u308b\u3002<br \/>\nargptr\u95a2\u6570\u306f\u3001\u6574\u6570\u3068\u3057\u3066\u5f15\u6570\u3092\u53d6\u3063\u3066\u304f\u308b\u305f\u3081\u306bargint\u95a2\u6570\u3092\u4f7f\u3044\u3001\u305d\u3057\u3066\u305d\u306e\u30e6\u30fc\u30b6\u30dd\u30a4\u30f3\u30bf\u3068\u3057\u3066\u306e\u6574\u6570\u304c\u30a2\u30c9\u30ec\u30b9\u7a7a\u9593\u306e\u30e6\u30fc\u30b6\u306e\u90e8\u5206\u3092\u6307\u3059\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3002<br \/>\nargptr\u3092\u547c\u3076\u30682\u56de\u306e\u30c1\u30a7\u30c3\u30af\u304c\u8d70\u308b\u3053\u3068\u306b\u6ce8\u610f\u3002<br \/>\n\u6700\u521d\u306b\u3001\u30e6\u30fc\u30b6\u306e\u30b9\u30bf\u30c3\u30af\u30dd\u30a4\u30f3\u30bf\u306f\u5f15\u6570\u3092\u53d6\u308a\u51fa\u3059\u3068\u304d\u306b\u30c1\u30a7\u30c3\u30af\u3055\u308c\u308b\u3002<br \/>\n\u305d\u3057\u3066\u5f15\u6570\u3001\u30e6\u30fc\u30b6\u30dd\u30a4\u30f3\u30bf\u305d\u308c\u81ea\u8eab\u306f\u30c1\u30a7\u30c3\u30af\u3055\u308c\u308b\u3002<\/p>\n<p>argstr\u95a2\u6570\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u5f15\u6570\u30c8\u30ea\u30aa\u306e\u6700\u5f8c\u306e\u30e1\u30f3\u30d0\u30fc\u3067\u3042\u308b\u3002<br \/>\n\u305d\u308c\u306f\u3001\u30dd\u30a4\u30f3\u30bf\u3068\u3057\u3066n\u756a\u76ee\u306e\u5f15\u6570\u3092\u89e3\u91c8\u3059\u308b\u3002<br \/>\n\u305d\u308c\u306f\u3001\u30dd\u30a4\u30f3\u30bf\u304c\u30cc\u30eb\u7d42\u7aef\u6587\u5b57\u5217\u3092\u6307\u3059\u3053\u3068\u3068\u3001\u305d\u306e\u6587\u5b57\u5217\u5168\u4f53\u304c\u30a2\u30c9\u30ec\u30b9\u7a7a\u9593\u306e\u30e6\u30fc\u30b6\u306e\u90e8\u5206\u306e\u7d42\u308f\u308a\u3088\u308a\u66f8\u3044\u306b\u914d\u7f6e\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3059\u308b\u3002<\/p>\n<p>\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5b9f\u88c5\uff08\u4f8b\u3048\u3070sysproc.c\u3084sysfile.c\uff09\u306f\u3001\u5178\u578b\u7684\u306a\u30e9\u30c3\u30d1\u95a2\u6570\u3067\u3042\u308b\u3002<br \/>\n\u305d\u308c\u3089\u306f\u3001argint, argptr, argstr\u3092\u4f7f\u3063\u3066\u5f15\u6570\u3092\u30c7\u30b3\u30fc\u30c9\u3057\u305f\u3042\u3068\u3001\u5b9f\u969b\u306e\u5b9f\u88c5\u3092\u547c\u3073\u51fa\u3059\u3002<br \/>\n\u7b2c1\u7ae0\u3067\u3001sys_exec\u306f\u305d\u308c\u3089\u306e\u95a2\u6570\u3092\u4f7f\u3063\u3066\u5f15\u6570\u3092\u89e3\u91c8\u3057\u305f\u3002<\/p>\n<p>sysproc.c\u3067\u5b9f\u88c5\u3055\u308c\u3066\u308b\u95a2\u6570\u306e\u6700\u521d\u306e3\u3064<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">int\r\nsys_fork(void)\r\n{\r\n  return fork();\r\n}\r\n\r\nint\r\nsys_exit(void)\r\n{\r\n  exit();\r\n  return 0;  \/\/ not reached\r\n}\r\n\r\nint\r\nsys_wait(void)\r\n{\r\n  return wait();\r\n}<\/pre>\n<h3>\u611f\u60f3<\/h3>\n<p>\u305f\u3044\u3057\u3066\u96e3\u3057\u3044\u3068\u3053\u308d\u3058\u3083\u306a\u3044\u3068\u601d\u3046\u3093\u3067\u3059\u304c\u3001\u4eca\u56de\u306f\u51c4\u304f\u672c\u6587\u304c\u8aad\u307f\u3065\u3089\u304b\u3063\u305f\u3067\u3059\u3002<br \/>\n\u898b\u6163\u308c\u306a\u3044\u6587\u6cd5\u3084\u9593\u9055\u3063\u3066\u308b\u3063\u307d\u3044\u5358\u8a9e\u3084\u56de\u308a\u304f\u3069\u3044\u66f8\u304d\u65b9\u304c\u3044\u304f\u3064\u304b\u3042\u308a\u307e\u3057\u305f\u3002<br \/>\n\u306a\u306e\u3067\u3001\u3044\u3064\u3082\u305d\u3046\u3067\u306f\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u4eca\u56de\u306f\u7279\u306b\u8a33\u304c\u3042\u3084\u3057\u3044\u3067\u3059\u3002<br \/>\n\u66f8\u3044\u305f\u4eba\u304c\u9055\u3046\u306e\u304b\u306a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e36\u301c37\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5834\u5408\u3001trap\u95a2\u6570\u306fsyscall\u95a2\u6570\u3092\u547c\u3076\u3002<br \/>\nsyscall\u95a2\u6570\u306f\u3001\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u304b\u3089\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u756a\u53f7\u3092\u8aad\u307f\u8fbc\u3080\u3002<br \/>\n\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u306f\u4fdd\u5b58\u3055\u308c\u305f%eax\u3092\u542b\u307f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30c6\u30fc\u30d6\u30eb\u306e\u7279\u5b9a\u306e\u30a8\u30f3\u30c8\u30ea\u3092\u6307\u3059\u3002<br \/>\n\u6700\u521d\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5834\u5408\u3001%eax\u306fSYS_exec\u3068\u3044\u3046\u5024\u3092\u542b\u3093\u3067\u3044\u308b\u3002\uff08#define SYS_exec 7\uff09<br \/>\n\u305d\u3057\u3066syscall\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30c6\u30fc\u30d6\u30eb\u306eSYS_exec\u756a\u76ee\u306e\u30a8\u30f3\u30c8\u30ea\u3092\u547c\u3073\u51fa\u3059\u3002<br \/>\n\u305d\u306e\u30a8\u30f3\u30c8\u30ea\u306f\u3001sys_exec\u95a2\u6570\u306e\u547c\u3073\u51fa\u3057\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u3002<\/p>\n<p>syscall.c\u306esyscall\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">void\r\nsyscall(void)\r\n{\r\n  int num;\r\n\r\n  num = proc-&gt;tf-&gt;eax;\r\n  if(num &gt;= 0 &amp;&amp; num &lt; SYS_open &amp;&amp; syscalls&#x5B;num]) {\r\n    proc-&gt;tf-&gt;eax = syscalls&#x5B;num]();\r\n  } else if (num &gt;= SYS_open &amp;&amp; num &lt; NELEM(syscalls) &amp;&amp; syscalls&#x5B;num]) {\r\n    proc-&gt;tf-&gt;eax = syscalls&#x5B;num]();\r\n  } else {\r\n    cprintf(&quot;%d %s: unknown sys call %d\\n&quot;,\r\n            proc-&gt;pid, proc-&gt;name, num);\r\n    proc-&gt;tf-&gt;eax = -1;\r\n  }\r\n}<\/pre>\n<p>syscall\u95a2\u6570\u306f\u3001%eax\u304c\u6307\u3059\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u95a2\u6570\u306e\u623b\u308a\u5024\u3092\u8a18\u9332\u3059\u308b\u3002<br \/>\n\u30c8\u30e9\u30c3\u30d7\u304b\u3089\u30e6\u30fc\u30b6\u30b9\u30da\u30fc\u30b9\u306b\u623b\u3063\u305f\u3068\u304d\u3001\u305d\u308c\u306fcp-&gt;tf\u304b\u3089\u30de\u30b7\u30f3\u306e\u30ec\u30b8\u30b9\u30bf\u306b\u30ed\u30fc\u30c9\u3055\u308c\u308b\u3060\u308d\u3046\u3002<br \/>\n\u3053\u306e\u3088\u3046\u306b\u3001exec\u304b\u3089\u623b\u3063\u305f\u3068\u304d\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30cf\u30f3\u30c9\u30e9\u304b\u3089\u8fd4\u3063\u305f\u5024\u3092\u8fd4\u3059\u3060\u308d\u3046\u3002\uff08syscall\u95a2\u6570\u4e2d\u306e\u6700\u521d\u306eproc\u2212&gt;tf\u2212&gt;eax = syscalls&#091;num&#093;();\u306e\u90e8\u5206\uff09<br \/>\n\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306f\u3001\u6163\u7fd2\u7684\u306b\u30a8\u30e9\u30fc\u3092\u901a\u77e5\u3059\u308b\u305f\u3081\u306b\u8ca0\u306e\u5024\u3092\u8fd4\u3057\u3001\u6210\u529f\u3057\u305f\u3068\u304d\u306f\u6b63\u306e\u5024\u3092\u8fd4\u3059\u3002<br \/>\n\u3082\u3057\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u756a\u53f7\u304c\u7570\u5e38\u306a\u3089\u3001syscall\u95a2\u6570\u306f\u30a8\u30e9\u30fc\u3092\u5370\u5b57\u3057-1\u3092\u8fd4\u3059\u3002<\/p>\n<p>\u5f8c\u306e\u7ae0\u3067\u3001\u500b\u5225\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5b9f\u88c5\u306b\u3064\u3044\u3066\u8aac\u660e\u3059\u308b\u3002<br \/>\n\u3053\u306e\u7ae0\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u30e1\u30ab\u30cb\u30ba\u30e0\u306b\u95a2\u4fc2\u3057\u3066\u3044\u308b\u3002<br \/>\n\u30e1\u30ab\u30cb\u30ba\u30e0\u304c\u5f8c\u4e00\u3064\u6b8b\u3063\u3066\u3044\u308b\u3002<br \/>\n\u305d\u308c\u306f\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5f15\u6570\u3092\u898b\u3064\u3051\u308b\u4ed5\u7d44\u307f\u306b\u3064\u3044\u3066\u3067\u3042\u308b\u3002<br \/>\nargint\u3068argptr\u3068argstr\u3068\u3044\u3046\u30d8\u30eb\u30d1\u95a2\u6570\u306f\u3001n\u756a\u76ee\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5f15\u6570\u3092\u53d6\u308a\u51fa\u3059\u3002<br \/>\n\u305d\u308c\u305e\u308c\u9806\u756a\u306b\u3001\u6574\u6570\u3001\u30dd\u30a4\u30f3\u30bf\u3001\u6587\u5b57\u5217\u306b\u5bfe\u5fdc\u3059\u308b\u3002<br \/>\nargint\u306f\u3001n\u756a\u76ee\u306e\u5f15\u6570\u3092\u7279\u5b9a\u3059\u308b\u305f\u3081\u306b\u3001\u30e6\u30fc\u30b6\u7a7a\u9593\u306e%esp\u30ec\u30b8\u30b9\u30bf\u3092\u4f7f\u3046\u3002<br \/>\n%esp\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u30b9\u30bf\u30d6\u306e\u305f\u3081\u306e\u623b\u308a\u5148\u30a2\u30c9\u30ec\u30b9\u3092\u6307\u3057\u3066\u3044\u308b\u3002<br \/>\n\u5f15\u6570\u306f\u3001\u307e\u3055\u306b\u305d\u306e\uff08\u623b\u308a\u5148\u30a2\u30c9\u30ec\u30b9\uff09\u306e\u3059\u3050\u4e0a\u4f4d\u306b\u3042\u308a\u3001\u305d\u308c\u306f%esp+4\u3067\u5c0e\u304d\u51fa\u305b\u308b\u3002<br \/>\n\u3060\u304b\u3089n\u756a\u76ee\u306e\u5f15\u6570\u306f\u3001%esp+4+4*n\u3067\u5c0e\u304d\u51fa\u305b\u308b\u3002<\/p>\n<p>syscall.c\u306eargint, argptr, argstr\u95a2\u9023\u306e\u90e8\u5206<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ User code makes a system call with INT T_SYSCALL.\r\n\/\/ System call number in %eax.\r\n\/\/ Arguments on the stack, from the user call to the C\r\n\/\/ library system call function. The saved user %esp points\r\n\/\/ to a saved program counter, and then the first argument.\r\n\r\n\/\/ Fetch the int at addr from process p.\r\nint\r\nfetchint(struct proc *p, uint addr, int *ip)\r\n{\r\n  if(addr &gt;= p-&gt;sz || addr+4 &gt; p-&gt;sz)\r\n    return -1;\r\n  *ip = *(int*)(addr);\r\n  return 0;\r\n}\r\n\r\n\/\/ Fetch the nul-terminated string at addr from process p.\r\n\/\/ Doesn't actually copy the string - just sets *pp to point at it.\r\n\/\/ Returns length of string, not including nul.\r\nint\r\nfetchstr(struct proc *p, uint addr, char **pp)\r\n{\r\n  char *s, *ep;\r\n\r\n  if(addr &gt;= p-&gt;sz)\r\n    return -1;\r\n  *pp = (char*)addr;\r\n  ep = (char*)p-&gt;sz;\r\n  for(s = *pp; s &lt; ep; s++)\r\n    if(*s == 0)\r\n      return s - *pp;\r\n  return -1;\r\n}\r\n\r\n\/\/ Fetch the nth 32-bit system call argument.\r\nint\r\nargint(int n, int *ip)\r\n{\r\n  return fetchint(proc, proc-&gt;tf-&gt;esp + 4 + 4*n, ip);\r\n}\r\n\r\n\/\/ Fetch the nth word-sized system call argument as a pointer\r\n\/\/ to a block of memory of size n bytes.  Check that the pointer\r\n\/\/ lies within the process address space.\r\nint\r\nargptr(int n, char **pp, int size)\r\n{\r\n  int i;\r\n  \r\n  if(argint(n, &amp;i) &lt; 0)\r\n    return -1;\r\n  if((uint)i &gt;= proc-&gt;sz || (uint)i+size &gt; proc-&gt;sz)\r\n    return -1;\r\n  *pp = (char*)i;\r\n  return 0;\r\n}\r\n\r\n\/\/ Fetch the nth word-sized system call argument as a string pointer.\r\n\/\/ Check that the pointer is valid and the string is nul-terminated.\r\n\/\/ (There is no shared writable memory, so the string can't change\r\n\/\/ between this check and being used by the kernel.)\r\nint\r\nargstr(int n, char **pp)\r\n{\r\n  int addr;\r\n  if(argint(n, &amp;addr) &lt; 0)\r\n    return -1;\r\n  return fetchstr(proc, addr, pp);\r\n}<\/pre>\n<p>argint\u95a2\u6570\u306f\u3001\u30e6\u30fc\u30b6\u30e1\u30e2\u30ea\u306b\u3042\u308b\u30a2\u30c9\u30ec\u30b9\u304b\u3089\u5024\u3092\u8aad\u307f\u8fbc\u307f\u3001*ip\u306b\u305d\u308c\u3092\u66f8\u304d\u8fbc\u3080\u305f\u3081\u306bfetchint\u95a2\u6570\u3092\u547c\u3076\u3002<br \/>\nfetchint\u306f\u3001\u305d\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u7c21\u5358\u306b\u30dd\u30a4\u30f3\u30bf\u306b\u30ad\u30e3\u30b9\u30c8\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br \/>\n\u306a\u305c\u306a\u3089\u3001\u30e6\u30fc\u30b6\u3068\u30ab\u30fc\u30cd\u30eb\u306f\u540c\u3058\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u3092\u5171\u6709\u3057\u3066\u3044\u308b\u304b\u3089\u3067\u3042\u308b\u3002<br \/>\n\u3057\u304b\u3057\u3001\u30ab\u30fc\u30cd\u30eb\u306f\u3001\u30e6\u30fc\u30b6\u306b\u3088\u308b\u305d\u306e\u30dd\u30a4\u30f3\u30bf\u304c\u30a2\u30c9\u30ec\u30b9\u7a7a\u9593\u306e\u30e6\u30fc\u30b6\u306e\u90e8\u5206\u306b\u3042\u308b\u30dd\u30a4\u30f3\u30bf\u3067\u3042\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\n\u30ab\u30fc\u30cd\u30eb\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u304c\u305d\u308c\u81ea\u8eab\u306e\u30ed\u30fc\u30ab\u30eb\u3067\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u306a\u30e1\u30e2\u30ea\u306e\u5916\u306b\u30a2\u30af\u30bb\u30b9\u51fa\u6765\u306a\u3044\u3053\u3068\u3092\u78ba\u5b9f\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u305f\u3002<br \/>\n\u3082\u3057\u3001\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u304cp-&gt;sz\u3088\u308a\u4e0a\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u8aad\u307f\u66f8\u304d\u3057\u3088\u3046\u3068\u3057\u305f\u3089\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u30bb\u30b0\u30e1\u30f3\u30c6\u30fc\u30b7\u30e7\u30f3\u30c8\u30e9\u30c3\u30d7\u3092\u5f15\u304d\u8d77\u3053\u3057\u3001\u305d\u3057\u3066\u305d\u306e\u30c8\u30e9\u30c3\u30d7\u306f\u30d7\u30ed\u30bb\u30b9\u3092\u6bba\u3059\u3002<br \/>\n\u6211\u3005\u304c\u3044\u307e\u307e\u3067\u898b\u3066\u304d\u305f\u3088\u3046\u306b\u3002<br \/>\n\u4eca\u3001\u30ab\u30fc\u30cd\u30eb\u306f\u5b9f\u884c\u4e2d\u3067\u3042\u308a\u3001\u30e6\u30fc\u30b6\u304c\u6e21\u3057\u305f\u304b\u3082\u3057\u308c\u306a\u3044\u3069\u306e\u30a2\u30c9\u30ec\u30b9\u3082\u9006\u53c2\u7167\u53ef\u80fd\u3067\u3042\u308b\u3001\u3068\u306f\u3044\u3048\u3001\u305d\u306e\u30a2\u30c9\u30ec\u30b9\u304cp-&gt;sz\u672a\u6e80\u3067\u3042\u308b\u3053\u3068\u3092\u30ab\u30fc\u30cd\u30eb\u306f\u78ba\u8a8d\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/p>\n<p>argptr\u95a2\u6570\u306f\u3001argint\u306e\u7528\u9014\u3068\u4f3c\u3066\u3044\u308b\u3002<br \/>\nargptr\u95a2\u6570\u306f\u3001n\u756a\u76ee\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5f15\u6570\u3092\u89e3\u91c8\u3059\u308b\u3002<br \/>\nargptr\u95a2\u6570\u306f\u3001\u6574\u6570\u3068\u3057\u3066\u5f15\u6570\u3092\u53d6\u3063\u3066\u304f\u308b\u305f\u3081\u306bargint\u95a2\u6570\u3092\u4f7f\u3044\u3001\u305d\u3057\u3066\u305d\u306e\u30e6\u30fc\u30b6\u30dd\u30a4\u30f3\u30bf\u3068\u3057\u3066\u306e\u6574\u6570\u304c\u30a2\u30c9\u30ec\u30b9\u7a7a\u9593\u306e\u30e6\u30fc\u30b6\u306e\u90e8\u5206\u3092\u6307\u3059\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3002<br \/>\nargptr\u3092\u547c\u3076\u30682\u56de\u306e\u30c1\u30a7\u30c3\u30af\u304c\u8d70\u308b\u3053\u3068\u306b\u6ce8\u610f\u3002<br \/>\n\u6700\u521d\u306b\u3001\u30e6\u30fc\u30b6\u306e\u30b9\u30bf\u30c3\u30af\u30dd\u30a4\u30f3\u30bf\u306f\u5f15\u6570\u3092\u53d6\u308a\u51fa\u3059\u3068\u304d\u306b\u30c1\u30a7\u30c3\u30af\u3055\u308c\u308b\u3002<br \/>\n\u305d\u3057\u3066\u5f15\u6570\u3001\u30e6\u30fc\u30b6\u30dd\u30a4\u30f3\u30bf\u305d\u308c\u81ea\u8eab\u306f\u30c1\u30a7\u30c3\u30af\u3055\u308c\u308b\u3002<\/p>\n<p>argstr\u95a2\u6570\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u5f15\u6570\u30c8\u30ea\u30aa\u306e\u6700\u5f8c\u306e\u30e1\u30f3\u30d0\u30fc\u3067\u3042\u308b\u3002<br \/>\n\u305d\u308c\u306f\u3001\u30dd\u30a4\u30f3\u30bf\u3068\u3057\u3066n\u756a\u76ee\u306e\u5f15\u6570\u3092\u89e3\u91c8\u3059\u308b\u3002<br \/>\n\u305d\u308c\u306f\u3001\u30dd\u30a4\u30f3\u30bf\u304c\u30cc\u30eb\u7d42\u7aef\u6587\u5b57\u5217\u3092\u6307\u3059\u3053\u3068\u3068\u3001\u305d\u306e\u6587\u5b57\u5217\u5168\u4f53\u304c\u30a2\u30c9\u30ec\u30b9\u7a7a\u9593\u306e\u30e6\u30fc\u30b6\u306e\u90e8\u5206\u306e\u7d42\u308f\u308a\u3088\u308a\u66f8\u3044\u306b\u914d\u7f6e\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3059\u308b\u3002<\/p>\n<p>\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5b9f\u88c5\uff08\u4f8b\u3048\u3070sysproc.c\u3084sysfile.c\uff09\u306f\u3001\u5178\u578b\u7684\u306a\u30e9\u30c3\u30d1\u95a2\u6570\u3067\u3042\u308b\u3002<br \/>\n\u305d\u308c\u3089\u306f\u3001argint, argptr, argstr\u3092\u4f7f\u3063\u3066\u5f15\u6570\u3092\u30c7\u30b3\u30fc\u30c9\u3057\u305f\u3042\u3068\u3001\u5b9f\u969b\u306e\u5b9f\u88c5\u3092\u547c\u3073\u51fa\u3059\u3002<br \/>\n\u7b2c1\u7ae0\u3067\u3001sys_exec\u306f\u305d\u308c\u3089\u306e\u95a2\u6570\u3092\u4f7f\u3063\u3066\u5f15\u6570\u3092\u89e3\u91c8\u3057\u305f\u3002<\/p>\n<p>sysproc.c\u3067\u5b9f\u88c5\u3055\u308c\u3066\u308b\u95a2\u6570\u306e\u6700\u521d\u306e3\u3064<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">int\r\nsys_fork(void)\r\n{\r\n  return fork();\r\n}\r\n\r\nint\r\nsys_exit(void)\r\n{\r\n  exit();\r\n  return 0;  \/\/ not reached\r\n}\r\n\r\nint\r\nsys_wait(void)\r\n{\r\n  return wait();\r\n}<\/pre>\n<h3>\u611f\u60f3<\/h3>\n<p>\u305f\u3044\u3057\u3066\u96e3\u3057\u3044\u3068\u3053\u308d\u3058\u3083\u306a\u3044\u3068\u601d\u3046\u3093\u3067\u3059\u304c\u3001\u4eca\u56de\u306f\u51c4\u304f\u672c\u6587\u304c\u8aad\u307f\u3065\u3089\u304b\u3063\u305f\u3067\u3059\u3002<br \/>\n\u898b\u6163\u308c\u306a\u3044\u6587\u6cd5\u3084\u9593\u9055\u3063\u3066\u308b\u3063\u307d\u3044\u5358\u8a9e\u3084\u56de\u308a\u304f\u3069\u3044\u66f8\u304d\u65b9\u304c\u3044\u304f\u3064\u304b\u3042\u308a\u307e\u3057\u305f\u3002<br \/>\n\u306a\u306e\u3067\u3001\u3044\u3064\u3082\u305d\u3046\u3067\u306f\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u4eca\u56de\u306f\u7279\u306b\u8a33\u304c\u3042\u3084\u3057\u3044\u3067\u3059\u3002<br \/>\n\u66f8\u3044\u305f\u4eba\u304c\u9055\u3046\u306e\u304b\u306a\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[405],"class_list":["post-1394","post","type-post","status-publish","format-standard","hentry","category-tech","tag-xv6"],"_links":{"self":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/comments?post=1394"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1394\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=1394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=1394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=1394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}