{"id":45,"date":"2024-03-09T14:50:45","date_gmt":"2024-03-09T06:50:45","guid":{"rendered":"http:\/\/104.46.213.183\/?p=45"},"modified":"2024-03-09T14:50:45","modified_gmt":"2024-03-09T06:50:45","slug":"lab-system-call","status":"publish","type":"post","link":"https:\/\/www.yewpo.top\/index.php\/45\/","title":{"rendered":"Lab system call"},"content":{"rendered":"<h1>Lab system call<\/h1>\n<h2>1 Using gdb(easy)<\/h2>\n<blockquote>\n<p>\u6478\u6478GDB<\/p>\n<\/blockquote>\n<h3>1.1 \u5982\u4f55\u8c03\u8bd5<\/h3>\n<p>\u5728\u5b9e\u9a8c\u76ee\u5f55\u4e0b\u8fd0\u884c<code>make qemu-gdb<\/code>\uff0c\u5e76\u4e14\u5728\u4e00\u4e2a\u65b0\u7684\u7ec8\u7aef\u4e2d\u5bfc\u822a\u81f3\u5b9e\u9a8c\u76ee\u5f55\uff0c\u8fd0\u884c<strong><code>gdb-multiarch<\/code><\/strong>\uff0c\u5e76\u4e14\u53ea\u80fd\u662f\u8fd9\u4e2a\u3002<\/p>\n<p>\u4f46\u662f\u4f1a\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff1a<\/p>\n<pre><code class=\"language-shell\">warning: File &quot;\/home\/ubuntu\/program\/xv6-labs-2022\/.gdbinit&quot; auto-loading has been declined by your `auto-load safe-path&#039; set to &quot;$debugdir:$datadir\/auto-load&quot;.\nTo enable execution of this file add\n        add-auto-load-safe-path \/home\/ubuntu\/program\/xv6-labs-2022\/.gdbinit\nline to your configuration file &quot;\/home\/ubuntu\/.gdbinit&quot;.\nTo completely disable this security protection add\n        set auto-load safe-path \/\nline to your configuration file &quot;\/home\/ubuntu\/.gdbinit&quot;.\nFor more information about this security protection see the\n&quot;Auto-loading safe path&quot; section in the GDB manual.  E.g., run from the shell:\n        info &quot;(gdb)Auto-loading safe path&quot;<\/code><\/pre>\n<p>\u6839\u636e\u63d0\u793a\uff0c\u53ea\u9700\u8981\u5728\u7528\u6237\u6587\u4ef6\u5939\u4e2d\u521b\u5efa<code>.gdbinit<\/code>\u6587\u4ef6\u5e76\u8f93\u5165<code>set auto-load safe-path \/<\/code>\u5373\u53ef\u3002\u4fdd\u5b58\u540e\u5c31\u53ef\u4ee5\u7f16\u8bd1\u4e86\u3002<\/p>\n<h3>1.2 \u9898\u76ee\u76f8\u5173\u8bbe\u95ee\u7684\u56de\u7b54<\/h3>\n<ol>\n<li>Looking at the backtrace output, which function called <code>syscall<\/code>?<\/li>\n<\/ol>\n<p>\u6839\u636ebacktrace\u7684\u53cd\u9988\uff0c<code>usertrap<\/code>\u51fd\u6570\u8c03\u7528\u4e86<code>systemcall<\/code>\u3002<\/p>\n<ol start=\"2\">\n<li>What is the value of <code>p-&gt;trapframe-&gt;a7<\/code> and what does that value represent? (Hint: look <code>user\/initcode.S<\/code>, the first user program xv6 starts.)<\/li>\n<\/ol>\n<p>\u6839\u636e<code>user\/initcode.S(11)<\/code>\u53ef\u4ee5\u786e\u5b9a\u73b0\u5728\u7684a7\u662f<code>SYS_exec<\/code>(7)\u3002<\/p>\n<ol start=\"3\">\n<li>What was the previous mode that the CPU was in?<\/li>\n<\/ol>\n<p>\u6253\u5370<code>sstatus<\/code>\u5bc4\u5b58\u5668\u4e2d\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u5f97\u5230<code>0x22<\/code>\uff0c\u4e8c\u8fdb\u5236<code>00100010<\/code>\uff0cSSP\uff08\u7b2c8\u4f4d\uff09\u4f4d\u503c\u4e3a0\u3002\u636e\u95ee\u9898\u63d0\u4f9b\u7684<code>riscv-privileged-202101203<\/code>\u4e2d\u7b2c63\u9875\u7684\u63cf\u8ff0<code>The SPP bit indicates the privilege level at which a hart was executing before entering supervisor mode.When a trap is taken, SPP is set to 0 if the trap originated from user mode, or 1 otherwise.<\/code>\uff0c0\u8868\u793a\u4ece\u7528\u6237\u6001\u89e6\u53d1\u4e86\u9677\u9631\u3002<\/p>\n<ol start=\"4\">\n<li>Write down the assembly instruction the kernel is panicing at. Which register corresponds to the varialable <code>num<\/code>?<\/li>\n<\/ol>\n<p>\u6839\u636e\u51fa\u9519\u4fe1\u606f\uff0c\u5b9a\u4f4d\u5230\u4e86\u6c47\u7f16\u7684\u8fd9\u4e00\u884c<code>80001ff4:\t00002683          \tlw\ta3,0(zero) # 0 &lt;_entry-0x80000000&gt;<\/code>\uff0c\u5e76\u53ef\u4ee5\u5f97\u5230<code>num<\/code>\u50a8\u5b58\u5728<code>a3<\/code>\u5bc4\u5b58\u4e0a\u7684\u3002<\/p>\n<ol start=\"5\">\n<li>Why does the kernel crash? Hint: look at figure 3-3 in the text; is address 0 mapped in the kernel address space? Is that confirmed by the value in <code>scause<\/code> above? <\/li>\n<\/ol>\n<p>\u56e0\u4e3a\u865a\u62df\u5185\u5b58\u5730\u57400\u5e76\u672a\u4e0e\u5185\u6838\u5730\u5740\u4ea7\u751f\u6620\u5c04\u3002<code>scause\t<\/code>\u7684\u503c\u8bbe\u4e3a\u4e86<code>0xd<\/code>\uff0c\u537313\uff0c\u67e5\u8be2\u53c2\u8003\u6587\u672c\uff0c<code>scause<\/code>\u5bc4\u5b58\u5668\u6700\u9ad8\u4f4d\u8868\u793a\u4e2d\u65ad\u7c7b\u578b\uff0c\u5269\u4e0b\u7684\u8868\u793a\u5f02\u5e38\u7801\u3002\u5728\u4e2d\u65ad\u7c7b\u578b\u4e3a0\uff0c\u5f02\u5e38\u7801\u4e3a13\u7684\u60c5\u51b5\u8868\u793a<code>Load Page Fault<\/code>\uff0c\u548c\u5b9e\u9645\u9519\u8bef\u76f8\u7b26\u3002<\/p>\n<ol start=\"6\">\n<li>What is the name of the binary that was running when the kernel paniced? What is its process id (<code>pid<\/code>)?<\/li>\n<\/ol>\n<p>\u6b64\u65f6\u8fd0\u884c\u7684\u662f<code>initcode<\/code>\uff0c\u8fdb\u7a0b\u53f7\u4e3a1\uff08\u6253\u5370p\u76f8\u5173\u4fe1\u606f\u5f97\u5230\uff09\u3002<\/p>\n<h2>2 system call tracing(moderate)<\/h2>\n<blockquote>\n<p>\u4fee\u6539\u5df2\u6709\u7684\u76f8\u5173\u4ee3\u7801\uff0c\u5b9e\u73b0\u7cfb\u7edf\u8c03\u7528<code>trace<\/code>\uff0c\u8be5\u7cfb\u7edf\u8c03\u7528\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u901a\u8fc7\u5224\u65ad\u8be5\u4f4d\u662f\u5426\u662f1\u6765\u5224\u65ad\u8981\u8ffd\u8e2a\u67d0\u4e2a\u7cfb\u7edf\u8c03\u7528\u7684\u4f7f\u7528\u3002<\/p>\n<\/blockquote>\n<h3>2.1 \u7cfb\u7edf\u8c03\u7528\u7684\u8fc7\u7a0b<\/h3>\n<p>\u5728\u8fdb\u7a0b\u89e6\u53d1\u7cfb\u7edf\u8c03\u7528\u7684\u65f6\u5019\uff0c\u4f1a\u4fee\u6539\u8fdb\u7a0b\u4fe1\u606f\u7ed3\u6784\u4e2d\u7684<code>trapframe-&gt;a7<\/code>\uff0c\u8be5\u6570\u503c\u5c31\u8868\u793a\u8981\u8c03\u7528\u7684\u7cfb\u7edf\u51fd\u6570\u7684\u7f16\u53f7\u3002\u4e4b\u540e\u8fd0\u884c<code>\/kernel\/syscall.h<\/code>\u4e2d\u7684<code>syscall()<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u901a\u8fc7\u5728<code>a7<\/code>\u4e2d\u4fdd\u5b58\u7684\u8c03\u7528\u51fd\u6570\u7684\u7f16\u53f7\u627e\u5230\u5728<code>kernel\/sysproc.c<\/code>\u4e2d\u5bf9\u5e94\u7684\u7cfb\u7edf\u51fd\u6570\uff0c\u5e76\u5c06\u7cfb\u7edf\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4fdd\u5b58\u5728<code>trapframe-&gt;a0<\/code>\u4e2d\u3002<\/p>\n<h3>2.2 \u6dfb\u52a0\u7cfb\u7edf\u8c03\u7528trace\u7684\u4fe1\u606f<\/h3>\n<p>\u6309\u7167\u9898\u76ee\u7ed9\u7684hint\uff0c\u9996\u5148\u4fee\u6539<code>Makefile<\/code>\u3002\u5728<code>user\/usys.pl<\/code>\u4e2d\u6dfb\u52a0<code>entry(&quot;trace&quot;);<\/code>\u3002\u5728<code>user\/user.h<\/code>\u4e2d\u6dfb\u52a0<code>int trace(int);<\/code>\u3002<code>kernel\/syscall.h<\/code>\u4e2d\u6dfb\u52a0<code>#define SYS_trace 22<\/code>\u3002\u5728<code>kernel\/sysproc.c<\/code>\u6dfb\u52a0<code>systrace()<\/code>\u51fd\u6570\uff0c\u5e76\u5b9e\u73b0\u529f\u80fd\u3002\u5728<code>kernel\/syscall.c<\/code>\u4e2d\u6dfb\u52a0<code>sys_trace<\/code>\u7684\u51fd\u6570\u6307\u9488\u3002<\/p>\n<h3>2.3 \u5b9e\u73b0trace\u76f8\u5173\u529f\u80fd<\/h3>\n<p>\u4e3a\u4e86\u5728\u5f53\u524d\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u4e2d\u8bb0\u5f55\u662f\u5426\u8981\u8ffd\u8e2a\u67d0\u4e9b\u7cfb\u7edf\u51fd\u6570\u7684\u8c03\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u5728<code>struct proc<\/code>\u4e2d\u6dfb\u52a0<code>int trace_s;<\/code>\u5c5e\u6027\uff0c\u8868\u793a\u8981\u8ffd\u8e2a\u7684\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\u7684\u7801\u56fe\u3002<\/p>\n<h4>2.3.1 \u5b9e\u73b0sys_trace<\/h4>\n<blockquote>\n<p>\u8be5\u51fd\u6570\u901a\u8fc7<code>argint<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u6001\u4f20\u9012\u7684\u53c2\u6570\uff1a\u8981\u8ffd\u8e2a\u7684\u7cfb\u7edf\u8c03\u7528\u7684\u7801\u56fe\u3002\u5e76\u83b7\u5f97\u81ea\u5df1\u5f53\u524d\u8fdb\u7a0b\u7684<code>proc<\/code>\u7ed3\u6784\u4f53\uff0c\u5c06\u6211\u4eec\u65b0\u5efa\u7684<code>trace_s<\/code>\u7528\u7801\u56fe\u6765\u8d4b\u503c\u3002<\/p>\n<p>\u6839\u636e\u9898\u76ee\u9700\u8981\uff0c\u8be5\u51fd\u6570\u8fd4\u56de\u503c\u4e3a0\u3002<\/p>\n<\/blockquote>\n<p>\u4ee3\u7801<\/p>\n<pre><code class=\"language-C\">uint64\nsys_trace(void)\n{\n  int trace_s;\n\n  argint(0, &amp;trace_s);\n\n  myproc()-&gt;trace_s = trace_s;\n\n  return 0;\n}<\/code><\/pre>\n<h4>2.3.2 \u4fee\u6539syscall<\/h4>\n<p>\u4e3a\u4e86\u5224\u65ad\u662f\u5426\u9700\u8981\u8ffd\u8e2a\uff0c\u6bcf\u8c03\u7528\u4e00\u6b21\uff0c\u5224\u65ad\u8be5\u4f4d\u662f\u5426\u4e3a1\uff0c\u6709\u5c31\u6253\u5370\u8ffd\u8e2a\u4fe1\u606f\uff08\u63d0\u524d\u7b97\u597d\u6bcf\u4e2a\u7cfb\u7edf\u8c03\u7528\u7684\u7801\u56fe\uff0c\u53ef\u4ee5\u63d0\u901f0.4s\uff09\u3002<\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-C\">static char *syscallnames[] = {\n[SYS_fork]    &quot;fork&quot;,\n[SYS_exit]    &quot;exit&quot;,\n[SYS_wait]    &quot;wait&quot;,\n[SYS_pipe]    &quot;pipe&quot;,\n[SYS_read]    &quot;read&quot;,\n[SYS_kill]    &quot;kill&quot;,\n[SYS_exec]    &quot;exec&quot;,\n[SYS_fstat]   &quot;fstat&quot;,\n[SYS_chdir]   &quot;chdir&quot;,\n[SYS_dup]     &quot;dup&quot;,\n[SYS_getpid]  &quot;getpid&quot;,\n[SYS_sbrk]    &quot;sbrk&quot;,\n[SYS_sleep]   &quot;sleep&quot;,\n[SYS_uptime]  &quot;uptime&quot;,\n[SYS_open]    &quot;open&quot;,\n[SYS_write]   &quot;write&quot;,\n[SYS_mknod]   &quot;mknod&quot;,\n[SYS_unlink]  &quot;unlink&quot;,\n[SYS_link]    &quot;link&quot;,\n[SYS_mkdir]   &quot;mkdir&quot;,\n[SYS_close]   &quot;close&quot;,\n[SYS_trace]   &quot;trace&quot;,\n};\n\nstatic int syscallmasks[] = {\n[SYS_fork]    1 &lt;&lt; SYS_fork,\n[SYS_exit]    1 &lt;&lt; SYS_exit,\n[SYS_wait]    1 &lt;&lt; SYS_wait,\n[SYS_pipe]    1 &lt;&lt; SYS_pipe,\n[SYS_read]    1 &lt;&lt; SYS_read,\n[SYS_kill]    1 &lt;&lt; SYS_kill,\n[SYS_exec]    1 &lt;&lt; SYS_exec,\n[SYS_fstat]   1 &lt;&lt; SYS_fstat,\n[SYS_chdir]   1 &lt;&lt; SYS_chdir,\n[SYS_dup]     1 &lt;&lt; SYS_dup,\n[SYS_getpid]  1 &lt;&lt; SYS_getpid,\n[SYS_sbrk]    1 &lt;&lt; SYS_sbrk,\n[SYS_sleep]   1 &lt;&lt; SYS_sleep,\n[SYS_uptime]  1 &lt;&lt; SYS_uptime,\n[SYS_open]    1 &lt;&lt; SYS_open,\n[SYS_write]   1 &lt;&lt; SYS_write,\n[SYS_mknod]   1 &lt;&lt; SYS_mknod,\n[SYS_unlink]  1 &lt;&lt; SYS_unlink,\n[SYS_link]    1 &lt;&lt; SYS_link,\n[SYS_mkdir]   1 &lt;&lt; SYS_mkdir,\n[SYS_close]   1 &lt;&lt; SYS_close,\n[SYS_trace]   1 &lt;&lt; SYS_trace,\n};\n\nvoid\nsyscall(void)\n{\n  int num;\n  struct proc *p = myproc();\n\n  num = p-&gt;trapframe-&gt;a7;\n  if(num &gt; 0 &amp;&amp; num &lt; NELEM(syscalls) &amp;&amp; syscalls[num]) {\n    \/\/ Use num to lookup the system call function for num, call it,\n    \/\/ and store its return value in p-&gt;trapframe-&gt;a0\n    p-&gt;trapframe-&gt;a0 = syscalls[num]();\n\n    if (p-&gt;trace_s &amp; syscallmasks[num]) {\n      printf(&quot;%d: syscall %s -&gt; %d\\n&quot;, p-&gt;pid, syscallnames[num], p-&gt;trapframe-&gt;a0);\n    }\n  } else {\n    printf(&quot;%d %s: unknown sys call %d\\n&quot;,\n            p-&gt;pid, p-&gt;name, num);\n    p-&gt;trapframe-&gt;a0 = -1;\n  }\n}<\/code><\/pre>\n<h4>2.3.3 \u4fee\u6539kernel\/proc.c<\/h4>\n<p>\u4e3a\u4e86\u80fd\u8ffd\u8e2a\u5b50\u8fdb\u7a0b\u4e2d\u7684\u7cfb\u7edf\u8c03\u7528\uff0c\u8981\u5728fork\u51fd\u6570\u4e2d\u590d\u5236<code>trace_s<\/code>\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"language-C\">np-&gt;trace_s = p-&gt;trace_s;<\/code><\/pre>\n<h3>2.4 \u5b8c\u6210\u5bf9trace\u7cfb\u7edf\u8c03\u7528\u7684\u6dfb\u52a0<\/h3>\n<p>\u901a\u8fc7\u6d4b\u8bd5\uff0c\u9664\u4e86\u68c0\u6d4b\u5b50\u8fdb\u7a0b\u7684\u7cfb\u7edf\u8c03\u7528\u8ffd\u8e2a\u7528\u4e8642\u79d2\u4ee5\u5916\uff0c\u5176\u4ed6\u6d4b\u8bd5\u901a\u8fc7\u3002\uff08\u611f\u89c9\u662f\u6309\u7167\u6b65\u9aa4\u8981\u6c42\u6765\u5199\u4e86\uff0c\u4f46\u662f\u5c31\u662f\u8981\u752842\u79d2\uff0c\u4e5f\u4e0d\u77e5\u9053\u80fd\u4e0d\u80fd\u4f18\u5316\uff0c\u4ec5\u4ec5\u505a\u4e86\u4e00\u4e2a\u5c0f\u4f18\u5316\uff0c\u5728\u5224\u65ad\u662f\u4e0d\u662f\u8981\u8ffd\u8e2a\u7684\u7cfb\u7edf\u8c03\u7528\u54ea\u91cc\uff09<\/p>\n<h2>3 Sysinfo(moderate)<\/h2>\n<blockquote>\n<p>\u6dfb\u52a0\u7cfb\u7edf\u8c03\u7528<code>sysinfo<\/code>\uff0c\u8be5\u8c03\u7528\u5411\u7528\u6237\u8fd4\u56de\u5f53\u524d\u7cfb\u7edf\u7684\u53ef\u7528\u7a7a\u95f4\u548c\u6b63\u5728\u4f7f\u7528\u7684\u8fdb\u7a0b\u6570\u3002<\/p>\n<\/blockquote>\n<h3>3.1 \u6dfb\u52a0sysinfo\u7684\u7cfb\u7edf\u8c03\u7528<\/h3>\n<p>\u8be5\u8fc7\u7a0b\u5982\u540c<code>2 trace<\/code>\u3002<\/p>\n<p>\u53ea\u8981\u6ce8\u610f\u9898\u76ee\u7ed9\u7684hint\u7684\u4e2d\u7684\uff1a<\/p>\n<pre><code>To declare the prototype for sysinfo() in user\/user.h you need predeclare the existence of struct sysinfo:\n\n    struct sysinfo;\n    int sysinfo(struct sysinfo *);<\/code><\/pre>\n<h3>3.2 \u5b9e\u73b0sysinfo\u7cfb\u7edf\u8c03\u7528\u529f\u80fd<\/h3>\n<h4>3.2.1 sysinfo\u7ed3\u6784\u4f53<\/h4>\n<pre><code class=\"language-C\">struct sysinfo {\n  uint64 freemem;   \/\/ amount of free memory (bytes)\n  uint64 nproc;     \/\/ number of process\n};<\/code><\/pre>\n<h4>3.2.2 \u83b7\u5f97\u7a7a\u95f2\u5185\u5b58\u7684\u5927\u5c0f<\/h4>\n<p>\u5728<code>kernel\/kalloc.c<\/code>\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53eb<code>kmem<\/code>\u7684\u94fe\u8868\uff0c\u8be5\u94fe\u8868\u8fde\u63a5\u4e86\u6bcf\u4e00\u4e2a\u7a7a\u95f2\u9875\uff0c\u6bcf\u4e2a\u7a7a\u95f2\u9875\u7684\u5927\u5c0f<code>PGSIZE(4096)<\/code>\u3002\u901a\u8fc7\u904d\u5386\u8fd9\u4e2a\u94fe\u8868\uff0c\u83b7\u5f97\u7a7a\u95f2\u9875\u9762\u7684\u4e2a\u6570\uff0c\u5c31\u53ef\u4ee5\u83b7\u5f97\u7a7a\u95f2\u9875\u7684\u5927\u5c0f\u3002<\/p>\n<pre><code class=\"language-C\">uint64\nfreesize(void)\n{\n  uint64 count = 0;\n\n  struct run *r;\n\n  r = kmem.freelist;\n\n  while (r) {\n    count += PGSIZE;\n    r = r-&gt;next;\n  }\n\n  return count;\n}<\/code><\/pre>\n<h4>3.2.3 \u83b7\u5f97\u7cfb\u7edf\u6b63\u5728\u4f7f\u7528\u7684\u8fdb\u7a0b\u7684\u4e2a\u6570<\/h4>\n<p>\u5728<code>kernel\/proc.c<\/code>\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>proc<\/code>\u8fdb\u7a0b\u6570\u7ec4\uff0c\u6bcf\u4e00\u4e2a\u662f\u4e00\u4e2a<code>proc<\/code>\u7ed3\u6784\u4f53\uff0c<code>proc.stat<\/code>\u4fdd\u5b58\u4e86\u8be5\u8fdb\u7a0b\u7684\u88c5\u592a\u3002\u901a\u8fc7\u904d\u5386\u8fd9\u4e2a\u6570\u7ec4\uff0c\u7edf\u8ba1\u8fdb\u7a0b\u72b6\u6001\u4e3a<code>UNUSED<\/code>\u7684\u4e2a\u6570\u3002<\/p>\n<pre><code class=\"language-C\">uint64\nprocused(void)\n{\n  uint64 count = 0;\n\n  struct proc *p;\n\n  for (p = proc; p &lt; &amp;proc[NPROC]; p++) {\n    if (p-&gt;state != UNUSED) {\n        count++;\n    }\n  }\n\n  return count;\n}<\/code><\/pre>\n<h4>3.2.4 \u5728<code>kernel\/defs.h<\/code>\u4e2d\u58f0\u660e\u8fd9\u4e24\u4e2a\u51fd\u6570<\/h4>\n<p><strong>extermely important here<\/strong><\/p>\n<h3>3.3 sys_sysinfo()<\/h3>\n<p>\u9996\u5148\u901a\u8fc7\u81ea\u5df1\u5199\u7684\u51fd\u6570\u83b7\u5f97\u4e24\u4e2a\u4fe1\u606f\uff0c\u7528<code>copyout()<\/code>\u51fd\u6570\u5c06\u8be5\u4fe1\u606f\u590d\u5236\u5230\u7528\u6237\u5185\u5b58\u4e0a\u3002<\/p>\n<h4>3.3.1 copyout<\/h4>\n<p>\u672c\u4eba\u7528<code>vscode<\/code>\u6839\u672c\u770b\u4e0d\u5230\u8be5\u51fd\u6570\u7684\u6e90\u4ee3\u7801\uff0c\u672c\u4eba\u7406\u89e3\u662f\uff0c\u4f20\u9012\u56db\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u7528\u6237\u865a\u62df\u5185\u5b58\u9875\u8868\u4f4d\u7f6e\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u8981\u590d\u5236\u5230\u7684\u7528\u6237\u7684\u5185\u5b58\u4f4d\u7f6e\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u8981\u590d\u5236\u7684\u5185\u5bb9\u7684\u8d77\u59cb\u5185\u5b58\u4f4d\u7f6e\uff0c\u7b2c4\u4e2a\u53c2\u6570\u4e3a\u8981\u590d\u5236\u7684\u5185\u5bb9\u7684\u5927\u5c0f\u3002<\/p>\n<p>\u5b98\u65b9\u6ce8\u91ca<\/p>\n<pre><code>\/\/ Copy from kernel to user.\n\/\/ Copy len bytes from src to virtual address dstva in a given page table.\n\/\/ Return 0 on success, -1 on error.<\/code><\/pre>\n<h4>3.3.2 \u5b9e\u73b0\u4ee3\u7801<\/h4>\n<pre><code class=\"language-C\">uint64\nsys_sysinfo(void) {\n\n  uint64 addr;\n\n  argaddr(0, &amp;addr);\n\n  struct sysinfo info;\n\n  info.freemem = freesize();\n  info.nproc = procused();\n\n  if (copyout(myproc()-&gt;pagetable, addr, (char *)&amp;info, sizeof(info)) &lt; 0) {\n    return -1;\n  }\n\n  return 0;\n}<\/code><\/pre>\n<p>\u81f3\u6b64\uff0c\u8fd9\u4e2a\u5b9e\u9a8c\u4e5f\u5b8c\u6210\u4e86\u3002<\/p>\n<h2>4 END<\/h2>\n<p>\u6574\u4e2a\u5b9e\u9a8c\u5b8c\u6210\u3002\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\u3002<\/p>\n<pre><code class=\"language-\\\">== Test answers-syscall.txt == answers-syscall.txt: OK \n== Test trace 32 grep == trace 32 grep: OK (1.5s) \n== Test trace all grep == trace all grep: OK (1.4s) \n== Test trace nothing == trace nothing: OK (1.3s) \n== Test trace children == trace children: OK (42.1s) \n== Test sysinfotest == sysinfotest: OK (6.4s) \n== Test time == \ntime: OK \nScore: 40\/40<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>xv6\u64cd\u4f5c\u7cfb\u7edf\u5b9e\u9a8c2\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[14],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-os","tag-xv6"],"_links":{"self":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":0,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}