{"id":1383,"date":"2012-03-01T17:57:43","date_gmt":"2012-02-29T23:57:43","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=1383"},"modified":"2012-03-14T11:21:50","modified_gmt":"2012-03-14T02:21:50","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%ae25-2","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/1383","title":{"rendered":"[xv6 #25] Chapter 2 &#8211; Traps, interrupts, and drivers &#8211; Code: Assembly trap handlers"},"content":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e34\u301c36\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>xv6\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306b\u30c8\u30e9\u30c3\u30d7\u3092\u5f15\u304d\u8d77\u3053\u3055\u305b\u308b\u3088\u3046\u306aint\u547d\u4ee4\u306b\u906d\u9047\u3057\u305f\u3068\u304d\u3001\u76ee\u7684\u306b\u53f6\u3063\u305f\u4f55\u3089\u304b\u306e\u51e6\u7406\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u3001x86\u306e\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\nx86\u306f256\u500b\u306e\u500b\u5225\u306e\u5272\u308a\u8fbc\u307f\u3092\u6271\u3046\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u5272\u308a\u8fbc\u307f0-31\u306f\u3001\u9664\u7b97\u30a8\u30e9\u30fc\u3084\u304a\u304b\u3057\u306a\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3092\u8a66\u307f\u305f\u3068\u304d\u306e\u3088\u3046\u306a\u3001\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u4f8b\u5916\u306e\u305f\u3081\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\nxv6\u306f\u300132\u500b\u306e\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u5272\u308a\u8fbc\u307f\u309232-63\u306e\u9818\u57df\u306b\u5272\u308a\u5f53\u3066\u3066\u3044\u3066\u300164\u3092\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u7528\u306e\u5272\u308a\u8fbc\u307f\u306b\u5272\u308a\u5f53\u3066\u3066\u3044\u308b\u3002<\/p>\n<p>tvinit\u95a2\u6570\u306f\u3001main\u95a2\u6570\u304b\u3089\u547c\u3070\u308c\u3001idt\u30c6\u30fc\u30d6\u30eb\u306e\u4e2d\u306e256\u500b\u306e\u30a8\u30f3\u30c8\u30ea\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u3002<br \/>\n\u5272\u308a\u8fbc\u307fi\u306f\u3001vectors[i]\u306e\u4e2d\u306e\u30a2\u30c9\u30ec\u30b9\u306b\u3042\u308b\u30b3\u30fc\u30c9\u306b\u3088\u3063\u3066\u5236\u5fa1\u3055\u308c\u308b\u3002<br \/>\n\u30a8\u30f3\u30c8\u30ea\u30dd\u30a4\u30f3\u30c8\u306f\u5168\u3066\u9055\u3046\u3002<br \/>\n\u306a\u305c\u306a\u3089xv6\u306f\u5272\u308a\u8fbc\u307f\u30cf\u30f3\u30c9\u30e9\u306b\u5bfe\u3057\u3066\u30c8\u30e9\u30c3\u30d7\u756a\u53f7\u3092\u63d0\u4f9b\u3057\u306a\u3044\u304b\u3089\u3067\u3042\u308b\u3002<br \/>\n\uff08\u3053\u3053\u8a33\u304c\u3042\u3084\u3057\u3044\u3002because the x86 provides does not provide the trap number to the interrupt handler.\uff09<br \/>\n256\u500b\u306e\u500b\u5225\u306e\u30cf\u30f3\u30c9\u30e9\u3092\u4f7f\u3046\u3068\u3044\u3046\u3053\u3068\u306f\u3001256\u500b\u306e\u5834\u5408\u3092\u898b\u5206\u3051\u308b\u552f\u4e00\u306e\u65b9\u6cd5\u3067\u3042\u308b\u3002<br \/>\n\uff08\u5b9f\u969b\u306b\u5272\u308a\u8fbc\u307f\u30cf\u30f3\u30c9\u30e9\u304c\u5b9f\u884c\u3055\u308c\u308b\u3068\u304d\u306b\u306f\u305d\u306e\u30cf\u30f3\u30c9\u30e9\u304b\u3089\u306f\u30c8\u30e9\u30c3\u30d7\u756a\u53f7\u3092\u5f97\u308b\u65b9\u6cd5\u304c\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u304b\u306a\u3002\uff09<\/p>\n<p>trap.c\u306etvinit\u95a2\u6570\u4ed8\u8fd1<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Interrupt descriptor table (shared by all CPUs).\nstruct gatedesc idt&#x5B;256];\nextern uint vectors&#x5B;];  \/\/ in vectors.S: array of 256 entry pointers\nstruct spinlock tickslock;\nuint ticks;\n\nvoid\ntvinit(void)\n{\n  int i;\n\n  for(i = 0; i &lt; 256; i++)\n    SETGATE(idt&amp;#91;i&amp;#93;, 0, SEG_KCODE&lt;&lt;3, vectors&amp;#91;i&amp;#93;, 0);\n  SETGATE(idt&amp;#91;T_SYSCALL&amp;#93;, 1, SEG_KCODE&lt;&lt;3, vectors&amp;#91;T_SYSCALL&amp;#93;, DPL_USER);\n  \n  initlock(&amp;tickslock, &quot;time&quot;);\n}&amp;#91;\/sourcecode&amp;#93;\n\nmain.c\u306emain\u95a2\u6570\n&amp;#91;sourcecode language=&quot;c&quot;&amp;#93;\/\/ Bootstrap processor starts running C code here.\n\/\/ Allocate a real stack and switch to it, first\n\/\/ doing some setup required for memory allocator to work.\nint\nmain(void)\n{\n  kvmalloc();      \/\/ kernel page table\n  mpinit();        \/\/ collect info about this machine\n  lapicinit(mpbcpu());\n  seginit();       \/\/ set up segments\n  cprintf(&quot;\\ncpu%d: starting xv6\\n\\n&quot;, cpu-&gt;id);\n  picinit();       \/\/ interrupt controller\n  ioapicinit();    \/\/ another interrupt controller\n  consoleinit();   \/\/ I\/O devices &amp; their interrupts\n  uartinit();      \/\/ serial port\n  pinit();         \/\/ process table\n  tvinit();        \/\/ trap vectors\n  binit();         \/\/ buffer cache\n  fileinit();      \/\/ file table\n  iinit();         \/\/ inode cache\n  ideinit();       \/\/ disk\n  if(!ismp)\n    timerinit();   \/\/ uniprocessor timer\n  startothers();    \/\/ start other processors (must come before kinit)\n  kinit();         \/\/ initialize memory allocator\n  userinit();      \/\/ first user process  (must come after kinit)\n  \/\/ Finish setting up this processor in mpmain.\n  mpmain();\n}<\/pre>\n<p>tvinit\u306f\u3001\u30e6\u30fc\u30b6\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u3092\u30c8\u30e9\u30c3\u30d7\u3059\u308b\u305f\u3081\u306b\u3060\u3051\u306bT_SYSCALL\u3092\u5236\u5fa1\u3059\u308b\u3002<br \/>\n\u305d\u308c\u306f\u30012\u756a\u76ee\u306e\u5f15\u6570\u306b1\u3068\u3044\u3046\u5024\u3092\u6e21\u3059\u4e8b\u306b\u3088\u3063\u3066&#8221;trap&#8221;\u30bf\u30a4\u30d7\u306e\u30b2\u30fc\u30c8\u3092\u6307\u5b9a\u3059\u308b\u3002<br \/>\n\u30c8\u30e9\u30c3\u30d7\u30b2\u30fc\u30c8\u306fFL\u30d5\u30e9\u30b0\u3092\u30af\u30ea\u30a2\u305b\u305a\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30cf\u30f3\u30c9\u30e9\u4e2d\u306b\u4ed6\u306e\u5272\u308a\u8fbc\u307f\u3092\u8a31\u53ef\u3059\u308b\u3002<\/p>\n<p>\u30ab\u30fc\u30cd\u30eb\u3082\u307e\u305f\u3001\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u76f4\u63a5\u7684\u306aint\u547d\u4ee4\u306b\u3088\u3066\u30c8\u30e9\u30c3\u30d7\u3092\u751f\u6210\u3067\u304d\u308b\u3088\u3046\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30b2\u30fc\u30c8\u306e\u6a29\u9650\u3092DPL_USER\u306b\u30bb\u30c3\u30c8\u3059\u308b\u3002<br \/>\nxv6\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u306bint\u547d\u4ee4\u3092\u4f7f\u3063\u3066\u4ed6\u306e\u5272\u308a\u8fbc\u307f\uff08\u4f8b\u3048\u3070\u3001\u30c7\u30d0\u30a4\u30b9\u306e\u5272\u308a\u8fbc\u307f\uff09\u3092\u5f15\u304d\u8d77\u3053\u3059\u3053\u3068\u3092\u8a31\u53ef\u3057\u306a\u3044\u3002<br \/>\n\u3082\u3057\u30d7\u30ed\u30bb\u30b9\u304c\u305d\u308c\u3092\u8a66\u307f\u305f\u3089\u3001\u4e00\u822c\u4fdd\u8b77\u9055\u53cd\u306b\u906d\u9047\u3059\u308b\u3060\u308d\u3046\u3002<br \/>\n\uff08\u305d\u3057\u305f\u3089vectors[13]\u306b\u51e6\u7406\u304c\u79fb\u308b\uff09<\/p>\n<p>\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u304b\u3089\u30ab\u30fc\u30cd\u30eb\u30e2\u30fc\u30c9\u306b\u4fdd\u8b77\u30ec\u30d9\u30eb\u3092\u5909\u66f4\u3057\u305f\u3068\u304d\u3001\u30e6\u30fc\u30b6\u30d7\u30ed\u30bb\u30b9\u306e\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u308f\u306a\u3044\u3088\u3046\u306b\u3059\u3079\u304d\u3067\u3042\u308b\u3002<br \/>\n\u306a\u305c\u306a\u3089\u3001\u305d\u306e\u5185\u5bb9\u304c\u304a\u304b\u3057\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u304b\u3082\u3057\u308c\u306a\u3044\u304b\u3089\u3067\u3042\u308b\u3002<br \/>\n\u30e6\u30fc\u30b6\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u30e6\u30fc\u30b6\u30e1\u30e2\u30ea\u3058\u3083\u306a\u3044\u90e8\u5206\u3092\u6307\u3059\u3088\u3046\u306a\u30a2\u30c9\u30ec\u30b9\u3092%esp\u306b\u30bb\u30c3\u30c8\u3059\u308b\u3088\u3046\u306a\u30d0\u30b0\u3084\u60aa\u610f\u3092\u6301\u3063\u3066\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u3002<br \/>\nxv6\u306f\u3001\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u304c\u30b9\u30bf\u30c3\u30af\u30bb\u30b0\u30e1\u30f3\u30c8\u30bb\u30ec\u30af\u30bf\u3068%esp\u306e\u305f\u3081\u306e\u65b0\u3057\u3044\u5024\u3092\u8aad\u307f\u8fbc\u3080\u4e8b\u3092\u901a\u3057\u3066\u3001\u30bf\u30b9\u30af\u30bb\u30b0\u30e1\u30f3\u30c8\u30c7\u30a3\u30b9\u30af\u30ea\u30d7\u30bf\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u4e8b\u306b\u3088\u3063\u3066\u3001\u30c8\u30e9\u30c3\u30d7\u4e2d\u306b\u30b9\u30bf\u30c3\u30af\u5207\u308a\u66ff\u3048\u3092\u5b9f\u884c\u3055\u305b\u308b\u3088\u3046\u3001x86\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u3092\u30d7\u30ed\u30b0\u30e9\u30e0\u3059\u308b\u3002<br \/>\nswitchuvm\u95a2\u6570\u306f\u3001\u30e6\u30fc\u30b6\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u3001\u30bf\u30b9\u30af\u30bb\u30b0\u30e1\u30f3\u30c8\u30c7\u30a3\u30b9\u30af\u30ea\u30d7\u30bf\u306b\u4fdd\u5b58\u3059\u308b\u3002<\/p>\n<p>vm.c\u306eswitchuvm\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Switch TSS and h\/w page table to correspond to process p.\nvoid\nswitchuvm(struct proc *p)\n{\n  pushcli();\n  cpu-&gt;gdt&#x5B;SEG_TSS] = SEG16(STS_T32A, &amp;cpu-&gt;ts, sizeof(cpu-&gt;ts)-1, 0);\n  cpu-&gt;gdt&#x5B;SEG_TSS].s = 0;\n  cpu-&gt;ts.ss0 = SEG_KDATA &lt;&lt; 3;\n  cpu-&gt;ts.esp0 = (uint)proc-&gt;kstack + KSTACKSIZE;\n  ltr(SEG_TSS &lt;&lt; 3);\n  if(p-&gt;pgdir == 0)\n    panic(&quot;switchuvm: no pgdir&quot;);\n  lcr3(v2p(p-&gt;pgdir));  \/\/ switch to new address space\n  popcli();\n}<\/pre>\n<p>\u30c8\u30e9\u30c3\u30d7\u304c\u767a\u751f\u3057\u305f\u3068\u304d\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u306f\u4ee5\u4e0b\u306e\u4e8b\u3092\u884c\u3046\u3002<br \/>\n\u3082\u3057\u30d7\u30ed\u30bb\u30c3\u30b5\u304c\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u3067\u5b9f\u884c\u4e2d\u3060\u3063\u305f\u3089\u3001\u30bf\u30b9\u30af\u30bb\u30b0\u30e1\u30f3\u30c8\u30c7\u30a3\u30b9\u30af\u30ea\u30d7\u30bf\u304b\u3089%esp\u3068%ss\u3092\u30ed\u30fc\u30c9\u3057\u3001\u30e6\u30fc\u30b6\u306e%ss\u3068%esp\u3092\u65b0\u3057\u3044\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002<br \/>\n\u3082\u3057\u30d7\u30ed\u30bb\u30c3\u30b5\u304c\u30ab\u30fc\u30cd\u30eb\u30e2\u30fc\u30c9\u3067\u5b9f\u884c\u4e2d\u3060\u3063\u305f\u3089\u3001\u4e0a\u8a18\u306e\u3088\u3046\u306a\u4e8b\u306f\u4f55\u3082\u884c\u308f\u306a\u3044\u3002<br \/>\n\u305d\u3057\u305f\u3089\u30d7\u30ed\u30bb\u30c3\u30b5\u306f%eflags\u3068%cs\u3068%eip\u30ec\u30b8\u30b9\u30bf\u3092\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002<br \/>\n\u3044\u304f\u3064\u304b\u306e\u30c8\u30e9\u30c3\u30d7\u3067\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u30a8\u30e9\u30fc\u8a9e\uff08error word\uff09\u3082\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002<br \/>\n\u305d\u3057\u305f\u3089\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u9069\u5207\u306aIDT\u30a8\u30f3\u30c8\u30ea\u304b\u3089%eip\u3068%cs\u3092\u8aad\u307f\u8fbc\u3080\u3002<\/p>\n<p>xv6\u306f\u3001IDT\u30a8\u30f3\u30c8\u30ea\u30dd\u30a4\u30f3\u30c8\u3078\u306e\u30a8\u30f3\u30c8\u30ea\u30dd\u30a4\u30f3\u30c8\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u306bPerl\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u4f7f\u3046\u3002<br \/>\n\u305d\u308c\u305e\u308c\u306e\u30a8\u30f3\u30c8\u30ea\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u304c\u3084\u3089\u306a\u304b\u3063\u305f\u3089\u30a8\u30e9\u30fc\u30b3\u30fc\u30c9\u3092\u30d7\u30c3\u30b7\u30e5\u3057\u3001\u5272\u308a\u8fbc\u307f\u756a\u53f7\u3092\u30d7\u30c3\u30b7\u30e5\u3057\u3001\u305d\u3057\u3066alltraps\u306b\u30b8\u30e3\u30f3\u30d7\u3059\u308b\u3002<\/p>\n<p>vectors.pl<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">#!\/usr\/bin\/perl -w\n\n# Generate vectors.S, the trap\/interrupt entry points.\n# There has to be one entry point per interrupt number\n# since otherwise there&#039;s no way for trap() to discover\n# the interrupt number.\n\nprint &quot;# generated by vectors.pl - do not edit\\n&quot;;\nprint &quot;# handlers\\n&quot;;\nprint &quot;.globl alltraps\\n&quot;;\nfor(my $i = 0; $i &lt; 256; $i++){\n    print &quot;.globl vector$i\\n&quot;;\n    print &quot;vector$i:\\n&quot;;\n    if(!($i == 8 || ($i &gt;= 10 &amp;&amp; $i &lt;= 14) || $i == 17)){\n        print &quot;  pushl \\$0\\n&quot;;\n    }\n    print &quot;  pushl \\$$i\\n&quot;;\n    print &quot;  jmp alltraps\\n&quot;;\n}\n\nprint &quot;\\n# vector table\\n&quot;;\nprint &quot;.data\\n&quot;;\nprint &quot;.globl vectors\\n&quot;;\nprint &quot;vectors:\\n&quot;;\nfor(my $i = 0; $i &lt; 256; $i++){\n    print &quot;  .long vector$i\\n&quot;;\n}\n\n# sample output:\n#   # handlers\n#   .globl alltraps\n#   .globl vector0\n#   vector0:\n#     pushl $0\n#     pushl $0\n#     jmp alltraps\n#   ...\n#   \n#   # vector table\n#   .data\n#   .globl vectors\n#   vectors:\n#     .long vector0\n#     .long vector1\n#     .long vector2\n#   ...&amp;#91;\/sourcecode&amp;#93;\n\nalltraps\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u30ec\u30b8\u30b9\u30bf\u306e\u4fdd\u5b58\u3092\u7d9a\u3051\u308b\u3002\n%ds, %es, %fs, %gs\u3001\u305d\u308c\u3068\u6c4e\u7528\u7684\u306a\u30ec\u30b8\u30b9\u30bf\u3092\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002\uff08alltraps\u306epushl %ds\u304b\u3089pushal\u306e\u90e8\u5206\uff09\n\u3053\u306e\u52aa\u529b\u306e\u7d50\u679c\u3001\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306b\u3001\u30c8\u30e9\u30c3\u30d7\u306e\u77ac\u9593\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u30ec\u30b8\u30b9\u30bf\u3092\u542b\u3080trapframe\u69cb\u9020\u4f53\u304c\u542b\u307e\u308c\u308b\u3002\uff08\u56f32\u22122\u3092\u53c2\u7167\uff09\n\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u3001%ss, %esp, %eflags, %cs, %eip\u3092\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002\n\u30d7\u30ed\u30bb\u30c3\u30b5\u3082\u3057\u304f\u306f\u30c8\u30e9\u30c3\u30d7\u30d9\u30af\u30bf\u306f\u30a8\u30e9\u30fc\u756a\u53f7\u3092\u30d7\u30c3\u30b7\u30e5\u3057\u3001alltraps\u306f\u6b8b\u308a\u3092\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002\n\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u306f\u3001\u30ab\u30fc\u30cd\u30eb\u304b\u3089\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u623b\u3063\u305f\u3068\u304d\u306b\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u30ec\u30b8\u30b9\u30bf\u3092\u5fa9\u5143\u3059\u308b\u306e\u306b\u5341\u5206\u306a\u5168\u3066\u306e\u60c5\u5831\u3092\u542b\u3080\u3002\n\u305d\u306e\u7d50\u679c\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u30c8\u30e9\u30c3\u30d7\u304c\u958b\u59cb\u3057\u305f\u6642\u70b9\u3068\u3061\u3087\u3046\u3069\u540c\u3058\u72b6\u614b\u3067\u7d9a\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002\n\u7b2c1\u7ae0\u3092\u601d\u3044\u51fa\u3059\u3068\u3001userinit\u306f\u3053\u306e\u30b4\u30fc\u30eb\u3092\u9054\u6210\u3059\u308b\u305f\u3081\u306b\u3001\u624b\u52d5\u3067\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u3092\u69cb\u7bc9\u3057\u3066\u3044\u308b\u3002\n\uff08\u56f31-3\u53c2\u7167\uff09\n\n\u56f32-2 \u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u4e0a\u306e\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\n&lt;a href=&quot;https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-03-01-7.47.48.png&quot;&gt;&lt;img src=&quot;https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-03-01-7.47.48-271x300.png&quot; alt=&quot;&quot; title=&quot;\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2012-03-01 7.47.48&quot; width=&quot;271&quot; height=&quot;300&quot; class=&quot;aligncenter size-medium wp-image-1384&quot; \/&gt;&lt;\/a&gt;\n\n\u56f31-3 \u65b0\u3057\u3044\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\uff08\u518d\u63b2\uff09\n&lt;a href=&quot;https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-02-19-9.26.23.png&quot;&gt;&lt;img src=&quot;https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-02-19-9.26.23-294x300.png&quot; alt=&quot;&quot; title=&quot;\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2012-02-19 9.26.23&quot; width=&quot;294&quot; height=&quot;300&quot; class=&quot;aligncenter size-medium wp-image-1288&quot; \/&gt;&lt;\/a&gt;\n\ntrapasm.S\n&#x5B;sourcecode]#include &quot;mmu.h&quot;\n\n  # vectors.S sends all traps here.\n.globl alltraps\nalltraps:\n  # Build trap frame.\n  pushl %ds\n  pushl %es\n  pushl %fs\n  pushl %gs\n  pushal\n  \n  # Set up data and per-cpu segments.\n  movw $(SEG_KDATA&lt;&lt;3), %ax\n  movw %ax, %ds\n  movw %ax, %es\n  movw $(SEG_KCPU&lt;&lt;3), %ax\n  movw %ax, %fs\n  movw %ax, %gs\n\n  # Call trap(tf), where tf=%esp\n  pushl %esp\n  call trap\n  addl $4, %esp\n\n  # Return falls through to trapret...\n.globl trapret\ntrapret:\n  popal\n  popl %gs\n  popl %fs\n  popl %es\n  popl %ds\n  addl $0x8, %esp  # trapno and errcode\n  iret&amp;#91;\/sourcecode&amp;#93;\n\nx86.h\u306etrapframe\u69cb\u9020\u4f53\n&amp;#91;sourcecode language=&quot;c&quot;&amp;#93;\/\/ Layout of the trap frame built on the stack by the\n\/\/ hardware and by trapasm.S, and passed to trap().\nstruct trapframe {\n  \/\/ registers as pushed by pusha\n  uint edi;\n  uint esi;\n  uint ebp;\n  uint oesp;      \/\/ useless &amp; ignored\n  uint ebx;\n  uint edx;\n  uint ecx;\n  uint eax;\n\n  \/\/ rest of trap frame\n  ushort gs;\n  ushort padding1;\n  ushort fs;\n  ushort padding2;\n  ushort es;\n  ushort padding3;\n  ushort ds;\n  ushort padding4;\n  uint trapno;\n\n  \/\/ below here defined by x86 hardware\n  uint err;\n  uint eip;\n  ushort cs;\n  ushort padding5;\n  uint eflags;\n\n  \/\/ below here only when crossing rings, such as from user to kernel\n  uint esp;\n  ushort ss;\n  ushort padding6;\n};&amp;#91;\/sourcecode&amp;#93;\n\n\u6700\u521d\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5834\u5408\u3001\u4fdd\u5b58\u3055\u308c\u305f%eip\u306f\u3001int\u547d\u4ee4\u306e\u76f4\u5f8c\u306e\u547d\u4ee4\u306e\u30a2\u30c9\u30ec\u30b9\u3067\u3042\u308b\u3002\n%cs\u306f\u30e6\u30fc\u30b6\u30b3\u30fc\u30c9\u306e\u30bb\u30b0\u30e1\u30f3\u30c8\u30bb\u30ec\u30af\u30bf\u3067\u3042\u308b\u3002\n%eflags\u306fint\u547d\u4ee4\u3092\u5b9f\u884c\u3057\u305f\u77ac\u9593\u306eeflags\u30ec\u30b8\u30b9\u30bf\u306e\u5185\u5bb9\u3067\u3042\u308b\u3002\n\u4fdd\u5b58\u3055\u308c\u305f\u6c4e\u7528\u30ec\u30b8\u30b9\u30bf\u306e\u4e00\u90e8\u306e\u3088\u3046\u306b\u3001alltraps\u306f\u3001\u30ab\u30fc\u30cd\u30eb\u304c\u5f8c\u3067\u8abf\u3079\u308b\u305f\u3081\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u756a\u53f7\u3092\u542b\u3080%eax\u3082\u307e\u305f\u4fdd\u5b58\u3059\u308b\u3002\n\n\u4eca\u3001\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u30ec\u30b8\u30b9\u30bf\u306f\u4fdd\u5b58\u3055\u308c\u3001alltraps\u306f\u30d7\u30ed\u30bb\u30c3\u30b5\u306b\u30ab\u30fc\u30cd\u30eb\u306eC\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3055\u305b\u308b\u305f\u3081\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3092\u5b8c\u4e86\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002\n\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u3001\u30cf\u30f3\u30c9\u30e9\u306b\u5165\u308b\u524d\u306b\u30bb\u30ec\u30af\u30bf%cs, %ss\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002\nalltraps\u306f%ds\u3068%es\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002\uff08alltraps\u306emovw $(SEG_KDATA&lt;&lt;3), %ax\u304b\u3089movw %ax, %es\u306e\u90e8\u5206\uff09\n\u305d\u308c\u304b\u3089\u3001CPU\u3054\u3068\u306e\u30c7\u30fc\u30bf\u30bb\u30b0\u30e1\u30f3\u30c8\u3067\u3042\u308bSEG_KCPU\u3092\u6307\u3059\u305f\u3081\u306b%fs\u3068%gs\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002\uff08alltraps\u306emovw $(SEG_KCPU&lt;&lt;3), %ax\u304b\u3089movw %ax, %gs\u306e\u90e8\u5206\uff09\n\n\u4e00\u5ea6\u30bb\u30b0\u30e1\u30f3\u30c8\u304c\u9069\u5207\u306b\u30bb\u30c3\u30c8\u3055\u308c\u305f\u3089\u3001alltraps\u306f\u3001C\u3067\u66f8\u304b\u308c\u305f\u30c8\u30e9\u30c3\u30d7\u30cf\u30f3\u30c9\u30e9\u3067\u3042\u308btrap\u95a2\u6570\uff08trap.c\uff09\u3092\u547c\u3076\u3053\u3068\u304c\u51fa\u6765\u308b\u3002\nalltraps\u306f\u3001\u69cb\u7bc9\u3055\u308c\u305f\u3070\u304b\u308a\u306e\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u3092\u6307\u3059%esp\u3092\u3001trap\u3078\u306e\u5f15\u6570\u3068\u3057\u3066\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002\uff08alltraps\u306epushl %esp\u306e\u90e8\u5206\uff09\n\u305d\u3057\u3066trap\u3092\u547c\u3076\u3002\uff08alltraps\u306ecall trap\u306e\u90e8\u5206\uff09\ntrap\u95a2\u6570\u304b\u3089\u8fd4\u3063\u3066\u304d\u305f\u5f8c\u3001alltraps\u306f\u3001\u30b9\u30bf\u30c3\u30af\u30dd\u30a4\u30f3\u30bf\u306b\u52a0\u7b97\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u30b9\u30bf\u30c3\u30af\u306e\u5f15\u6570\u3092\u53d6\u308a\u9664\u304d\u3001\u305d\u3057\u3066\u3001trapret\u3068\u30e9\u30d9\u30eb\u3065\u3051\u3055\u308c\u305f\u30b3\u30fc\u30c9\u306e\u5b9f\u884c\u3092\u958b\u59cb\u3059\u308b\u3002\n\u6211\u3005\u306f\u3001\u7b2c1\u7ae0\u3067\u3001\u6700\u521d\u306e\u30e6\u30fc\u30b6\u30d7\u30ed\u30bb\u30b9\u304c\u30e6\u30fc\u30b6\u7a7a\u9593\u306b\u629c\u3051\u308b\u305f\u3081\u306btrapret\u3092\u5b9f\u884c\u3057\u305f\u3068\u304d\u3001\u3053\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u3063\u305f\u3002\n\u540c\u3058\u51e6\u7406\u304c\u3053\u3053\u3067\u8d77\u304d\u305f\u3002\n\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u3092\u901a\u3058\u3066\u30dd\u30c3\u30d7\u3059\u308b\u4e8b\u306f\u3001\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u306e\u30ec\u30b8\u30b9\u30bf\u3092\u5fa9\u5143\u3057\u3001\u305d\u3057\u3066iret\u306f\u30e6\u30fc\u30b6\u7a7a\u9593\u306b\u623b\u3059\u3002\n\ntrap.c\u306etrap\u95a2\u6570\n&amp;#91;sourcecode language=&quot;c&quot;&amp;#93;void\ntrap(struct trapframe *tf)\n{\n  if(tf-&gt;trapno == T_SYSCALL){\n    if(proc-&gt;killed)\n      exit();\n    proc-&gt;tf = tf;\n    syscall();\n    if(proc-&gt;killed)\n      exit();\n    return;\n  }\n\n  switch(tf-&gt;trapno){\n  case T_IRQ0 + IRQ_TIMER:\n    if(cpu-&gt;id == 0){\n      acquire(&amp;tickslock);\n      ticks++;\n      wakeup(&amp;ticks);\n      release(&amp;tickslock);\n    }\n    lapiceoi();\n    break;\n  case T_IRQ0 + IRQ_IDE:\n    ideintr();\n    lapiceoi();\n    break;\n  case T_IRQ0 + IRQ_IDE+1:\n    \/\/ Bochs generates spurious IDE1 interrupts.\n    break;\n  case T_IRQ0 + IRQ_KBD:\n    kbdintr();\n    lapiceoi();\n    break;\n  case T_IRQ0 + IRQ_COM1:\n    uartintr();\n    lapiceoi();\n    break;\n  case T_IRQ0 + 7:\n  case T_IRQ0 + IRQ_SPURIOUS:\n    cprintf(&quot;cpu%d: spurious interrupt at %x:%x\\n&quot;,\n            cpu-&gt;id, tf-&gt;cs, tf-&gt;eip);\n    lapiceoi();\n    break;\n   \n  \/\/PAGEBREAK: 13\n  default:\n    if(proc == 0 || (tf-&gt;cs&amp;3) == 0){\n      \/\/ In kernel, it must be our mistake.\n      cprintf(&quot;unexpected trap %d from cpu %d eip %x (cr2=0x%x)\\n&quot;,\n              tf-&gt;trapno, cpu-&gt;id, tf-&gt;eip, rcr2());\n      panic(&quot;trap&quot;);\n    }\n    \/\/ In user space, assume process misbehaved.\n    cprintf(&quot;pid %d %s: trap %d err %d on cpu %d &quot;\n            &quot;eip 0x%x addr 0x%x--kill proc\\n&quot;,\n            proc-&gt;pid, proc-&gt;name, tf-&gt;trapno, tf-&gt;err, cpu-&gt;id, tf-&gt;eip, \n            rcr2());\n    proc-&gt;killed = 1;\n  }\n\n  \/\/ Force process exit if it has been killed and is in user space.\n  \/\/ (If it is still executing in the kernel, let it keep running \n  \/\/ until it gets to the regular system call return.)\n  if(proc &amp;&amp; proc-&gt;killed &amp;&amp; (tf-&gt;cs&amp;3) == DPL_USER)\n    exit();\n\n  \/\/ Force process to give up CPU on clock tick.\n  \/\/ If interrupts were on while locks held, would need to check nlock.\n  if(proc &amp;&amp; proc-&gt;state == RUNNING &amp;&amp; tf-&gt;trapno == T_IRQ0+IRQ_TIMER)\n    yield();\n\n  \/\/ Check if the process has been killed since we yielded\n  if(proc &amp;&amp; proc-&gt;killed &amp;&amp; (tf-&gt;cs&amp;3) == DPL_USER)\n    exit();\n}<\/pre>\n<p>\u3053\u3053\u307e\u3067\u306e\u89e3\u8aac\u306f\u3001\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u304b\u3089\u767a\u751f\u3057\u305f\u30c8\u30e9\u30c3\u30d7\u306b\u3064\u3044\u3066\u8a71\u3057\u3066\u304d\u305f\u3002<br \/>\n\u3057\u304b\u3057\u30c8\u30e9\u30c3\u30d7\u306f\u30ab\u30fc\u30cd\u30eb\u3092\u5b9f\u884c\u3057\u3066\u308b\u3068\u304d\u3067\u3082\u767a\u751f\u3059\u308b\u3002<br \/>\n\u305d\u306e\u3088\u3046\u306a\u5834\u5408\u3001\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u306f\u30b9\u30bf\u30c3\u30af\u3092\u5207\u308a\u66ff\u3048\u305f\u308a\u3001\u30b9\u30bf\u30c3\u30af\u30dd\u30a4\u30f3\u30bf\u3084\u30b9\u30bf\u30c3\u30af\u30bb\u30b0\u30e1\u30f3\u30c8\u30bb\u30ec\u30af\u30bf\u3092\u4fdd\u5b58\u3057\u305f\u308a\u306f\u3057\u306a\u3044\u3002<br \/>\n\u3057\u304b\u3057\u306a\u304c\u3089\u3001\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u304b\u3089\u306e\u30c8\u30e9\u30c3\u30d7\u3068\u540c\u3058\u624b\u9806\u304c\u767a\u751f\u3057\u3001\u305d\u3057\u3066\u540c\u3058xv6\u306e\u30c8\u30e9\u30c3\u30d7\u3092\u5236\u5fa1\u3059\u308b\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002<br \/>\niret\u306e\u5f8c\u3001\u30ab\u30fc\u30cd\u30eb\u30e2\u30fc\u30c9\u306e%cs\u3092\u5fa9\u5143\u3057\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u30ab\u30fc\u30cd\u30eb\u30e2\u30fc\u30c9\u3067\u306e\u5b9f\u884c\u3092\u7d9a\u3051\u308b\u3002<\/p>\n<h3>\u611f\u60f3<\/h3>\n<p>\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u3067\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u3057\u305f\u3068\u304d\u306b\u767a\u751f\u3059\u308b\u30c8\u30e9\u30c3\u30d7\u3067\u3001\u3069\u3046\u3084\u3063\u3066\u30ab\u30fc\u30cd\u30eb\u30e2\u30fc\u30c9\u306b\u79fb\u884c\u3057\u3066\u3069\u3046\u3084\u3063\u3066\u623b\u308b\u304b\u306e\u8a71\u3067\u3059\u306d\u3002<\/p>\n<p>\u672c\u6587\u306b\u3082\u66f8\u3044\u3066\u3042\u308b\u3051\u3069\u3001vectors.S\u306fvectors.pl\u304b\u3089\u751f\u6210\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u308b\u306e\u3067\u3001\u4e00\u5ea6make\u3068\u304b\u3057\u306a\u3044\u3068\u5b58\u5728\u3057\u307e\u305b\u3093\u3002<\/p>\n<p>\u30bf\u30b9\u30af\u30bb\u30b0\u30e1\u30f3\u30c8\u30c7\u30a3\u30b9\u30af\u30ea\u30d7\u30bf\u5468\u308a\u306e\u8a71\u304c\u3088\u304f\u308f\u304b\u308a\u307e\u305b\u3093\u3002<br \/>\n<a href=\"http:\/\/ja.wikipedia.org\/wiki\/%E3%83%97%E3%83%AD%E3%83%86%E3%82%AF%E3%83%88%E3%83%A2%E3%83%BC%E3%83%89\" target=\"_blank\">\u30d7\u30ed\u30c6\u30af\u30c8\u30e2\u30fc\u30c9 &#8211; Wikipedia<\/a>\u3092\u8aad\u3081\u3070\u5c11\u3057\u306f\u5206\u304b\u308b\u304b\u306a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e34\u301c36\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>xv6\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306b\u30c8\u30e9\u30c3\u30d7\u3092\u5f15\u304d\u8d77\u3053\u3055\u305b\u308b\u3088\u3046\u306aint\u547d\u4ee4\u306b\u906d\u9047\u3057\u305f\u3068\u304d\u3001\u76ee\u7684\u306b\u53f6\u3063\u305f\u4f55\u3089\u304b\u306e\u51e6\u7406\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u3001x86\u306e\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\nx86\u306f256\u500b\u306e\u500b\u5225\u306e\u5272\u308a\u8fbc\u307f\u3092\u6271\u3046\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u5272\u308a\u8fbc\u307f0-31\u306f\u3001\u9664\u7b97\u30a8\u30e9\u30fc\u3084\u304a\u304b\u3057\u306a\u30e1\u30e2\u30ea\u30a2\u30c9\u30ec\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3092\u8a66\u307f\u305f\u3068\u304d\u306e\u3088\u3046\u306a\u3001\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u4f8b\u5916\u306e\u305f\u3081\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\nxv6\u306f\u300132\u500b\u306e\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u5272\u308a\u8fbc\u307f\u309232-63\u306e\u9818\u57df\u306b\u5272\u308a\u5f53\u3066\u3066\u3044\u3066\u300164\u3092\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u7528\u306e\u5272\u308a\u8fbc\u307f\u306b\u5272\u308a\u5f53\u3066\u3066\u3044\u308b\u3002<\/p>\n<p>tvinit\u95a2\u6570\u306f\u3001main\u95a2\u6570\u304b\u3089\u547c\u3070\u308c\u3001idt\u30c6\u30fc\u30d6\u30eb\u306e\u4e2d\u306e256\u500b\u306e\u30a8\u30f3\u30c8\u30ea\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u3002<br \/>\n\u5272\u308a\u8fbc\u307fi\u306f\u3001vectors[i]\u306e\u4e2d\u306e\u30a2\u30c9\u30ec\u30b9\u306b\u3042\u308b\u30b3\u30fc\u30c9\u306b\u3088\u3063\u3066\u5236\u5fa1\u3055\u308c\u308b\u3002<br \/>\n\u30a8\u30f3\u30c8\u30ea\u30dd\u30a4\u30f3\u30c8\u306f\u5168\u3066\u9055\u3046\u3002<br \/>\n\u306a\u305c\u306a\u3089xv6\u306f\u5272\u308a\u8fbc\u307f\u30cf\u30f3\u30c9\u30e9\u306b\u5bfe\u3057\u3066\u30c8\u30e9\u30c3\u30d7\u756a\u53f7\u3092\u63d0\u4f9b\u3057\u306a\u3044\u304b\u3089\u3067\u3042\u308b\u3002<br \/>\n\uff08\u3053\u3053\u8a33\u304c\u3042\u3084\u3057\u3044\u3002because the x86 provides does not provide the trap number to the interrupt handler.\uff09<br \/>\n256\u500b\u306e\u500b\u5225\u306e\u30cf\u30f3\u30c9\u30e9\u3092\u4f7f\u3046\u3068\u3044\u3046\u3053\u3068\u306f\u3001256\u500b\u306e\u5834\u5408\u3092\u898b\u5206\u3051\u308b\u552f\u4e00\u306e\u65b9\u6cd5\u3067\u3042\u308b\u3002<br \/>\n\uff08\u5b9f\u969b\u306b\u5272\u308a\u8fbc\u307f\u30cf\u30f3\u30c9\u30e9\u304c\u5b9f\u884c\u3055\u308c\u308b\u3068\u304d\u306b\u306f\u305d\u306e\u30cf\u30f3\u30c9\u30e9\u304b\u3089\u306f\u30c8\u30e9\u30c3\u30d7\u756a\u53f7\u3092\u5f97\u308b\u65b9\u6cd5\u304c\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u304b\u306a\u3002\uff09<\/p>\n<p>trap.c\u306etvinit\u95a2\u6570\u4ed8\u8fd1<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Interrupt descriptor table (shared by all CPUs).\nstruct gatedesc idt&#x5B;256];\nextern uint vectors&#x5B;];  \/\/ in vectors.S: array of 256 entry pointers\nstruct spinlock tickslock;\nuint ticks;\n\nvoid\ntvinit(void)\n{\n  int i;\n\n  for(i = 0; i &lt; 256; i++)\n    SETGATE(idt&amp;#91;i&amp;#93;, 0, SEG_KCODE&lt;&lt;3, vectors&amp;#91;i&amp;#93;, 0);\n  SETGATE(idt&amp;#91;T_SYSCALL&amp;#93;, 1, SEG_KCODE&lt;&lt;3, vectors&amp;#91;T_SYSCALL&amp;#93;, DPL_USER);\n  \n  initlock(&amp;tickslock, &quot;time&quot;);\n}&amp;#91;\/sourcecode&amp;#93;\n\nmain.c\u306emain\u95a2\u6570\n&amp;#91;sourcecode language=&quot;c&quot;&amp;#93;\/\/ Bootstrap processor starts running C code here.\n\/\/ Allocate a real stack and switch to it, first\n\/\/ doing some setup required for memory allocator to work.\nint\nmain(void)\n{\n  kvmalloc();      \/\/ kernel page table\n  mpinit();        \/\/ collect info about this machine\n  lapicinit(mpbcpu());\n  seginit();       \/\/ set up segments\n  cprintf(&quot;\\ncpu%d: starting xv6\\n\\n&quot;, cpu-&gt;id);\n  picinit();       \/\/ interrupt controller\n  ioapicinit();    \/\/ another interrupt controller\n  consoleinit();   \/\/ I\/O devices &amp; their interrupts\n  uartinit();      \/\/ serial port\n  pinit();         \/\/ process table\n  tvinit();        \/\/ trap vectors\n  binit();         \/\/ buffer cache\n  fileinit();      \/\/ file table\n  iinit();         \/\/ inode cache\n  ideinit();       \/\/ disk\n  if(!ismp)\n    timerinit();   \/\/ uniprocessor timer\n  startothers();    \/\/ start other processors (must come before kinit)\n  kinit();         \/\/ initialize memory allocator\n  userinit();      \/\/ first user process  (must come after kinit)\n  \/\/ Finish setting up this processor in mpmain.\n  mpmain();\n}<\/pre>\n<p>tvinit\u306f\u3001\u30e6\u30fc\u30b6\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u3092\u30c8\u30e9\u30c3\u30d7\u3059\u308b\u305f\u3081\u306b\u3060\u3051\u306bT_SYSCALL\u3092\u5236\u5fa1\u3059\u308b\u3002<br \/>\n\u305d\u308c\u306f\u30012\u756a\u76ee\u306e\u5f15\u6570\u306b1\u3068\u3044\u3046\u5024\u3092\u6e21\u3059\u4e8b\u306b\u3088\u3063\u3066&#8221;trap&#8221;\u30bf\u30a4\u30d7\u306e\u30b2\u30fc\u30c8\u3092\u6307\u5b9a\u3059\u308b\u3002<br \/>\n\u30c8\u30e9\u30c3\u30d7\u30b2\u30fc\u30c8\u306fFL\u30d5\u30e9\u30b0\u3092\u30af\u30ea\u30a2\u305b\u305a\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30cf\u30f3\u30c9\u30e9\u4e2d\u306b\u4ed6\u306e\u5272\u308a\u8fbc\u307f\u3092\u8a31\u53ef\u3059\u308b\u3002<\/p>\n<p>\u30ab\u30fc\u30cd\u30eb\u3082\u307e\u305f\u3001\u30e6\u30fc\u30b6\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u76f4\u63a5\u7684\u306aint\u547d\u4ee4\u306b\u3088\u3066\u30c8\u30e9\u30c3\u30d7\u3092\u751f\u6210\u3067\u304d\u308b\u3088\u3046\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u30b2\u30fc\u30c8\u306e\u6a29\u9650\u3092DPL_USER\u306b\u30bb\u30c3\u30c8\u3059\u308b\u3002<br \/>\nxv6\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u306bint\u547d\u4ee4\u3092\u4f7f\u3063\u3066\u4ed6\u306e\u5272\u308a\u8fbc\u307f\uff08\u4f8b\u3048\u3070\u3001\u30c7\u30d0\u30a4\u30b9\u306e\u5272\u308a\u8fbc\u307f\uff09\u3092\u5f15\u304d\u8d77\u3053\u3059\u3053\u3068\u3092\u8a31\u53ef\u3057\u306a\u3044\u3002<br \/>\n\u3082\u3057\u30d7\u30ed\u30bb\u30b9\u304c\u305d\u308c\u3092\u8a66\u307f\u305f\u3089\u3001\u4e00\u822c\u4fdd\u8b77\u9055\u53cd\u306b\u906d\u9047\u3059\u308b\u3060\u308d\u3046\u3002<br \/>\n\uff08\u305d\u3057\u305f\u3089vectors[13]\u306b\u51e6\u7406\u304c\u79fb\u308b\uff09<\/p>\n<p>\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u304b\u3089\u30ab\u30fc\u30cd\u30eb\u30e2\u30fc\u30c9\u306b\u4fdd\u8b77\u30ec\u30d9\u30eb\u3092\u5909\u66f4\u3057\u305f\u3068\u304d\u3001\u30e6\u30fc\u30b6\u30d7\u30ed\u30bb\u30b9\u306e\u30b9\u30bf\u30c3\u30af\u3092\u4f7f\u308f\u306a\u3044\u3088\u3046\u306b\u3059\u3079\u304d\u3067\u3042\u308b\u3002<br \/>\n\u306a\u305c\u306a\u3089\u3001\u305d\u306e\u5185\u5bb9\u304c\u304a\u304b\u3057\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u304b\u3082\u3057\u308c\u306a\u3044\u304b\u3089\u3067\u3042\u308b\u3002<br \/>\n\u30e6\u30fc\u30b6\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u30e6\u30fc\u30b6\u30e1\u30e2\u30ea\u3058\u3083\u306a\u3044\u90e8\u5206\u3092\u6307\u3059\u3088\u3046\u306a\u30a2\u30c9\u30ec\u30b9\u3092%esp\u306b\u30bb\u30c3\u30c8\u3059\u308b\u3088\u3046\u306a\u30d0\u30b0\u3084\u60aa\u610f\u3092\u6301\u3063\u3066\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u3002<br \/>\nxv6\u306f\u3001\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u304c\u30b9\u30bf\u30c3\u30af\u30bb\u30b0\u30e1\u30f3\u30c8\u30bb\u30ec\u30af\u30bf\u3068%esp\u306e\u305f\u3081\u306e\u65b0\u3057\u3044\u5024\u3092\u8aad\u307f\u8fbc\u3080\u4e8b\u3092\u901a\u3057\u3066\u3001\u30bf\u30b9\u30af\u30bb\u30b0\u30e1\u30f3\u30c8\u30c7\u30a3\u30b9\u30af\u30ea\u30d7\u30bf\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u4e8b\u306b\u3088\u3063\u3066\u3001\u30c8\u30e9\u30c3\u30d7\u4e2d\u306b\u30b9\u30bf\u30c3\u30af\u5207\u308a\u66ff\u3048\u3092\u5b9f\u884c\u3055\u305b\u308b\u3088\u3046\u3001x86\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u3092\u30d7\u30ed\u30b0\u30e9\u30e0\u3059\u308b\u3002<br \/>\nswitchuvm\u95a2\u6570\u306f\u3001\u30e6\u30fc\u30b6\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u3001\u30bf\u30b9\u30af\u30bb\u30b0\u30e1\u30f3\u30c8\u30c7\u30a3\u30b9\u30af\u30ea\u30d7\u30bf\u306b\u4fdd\u5b58\u3059\u308b\u3002<\/p>\n<p>vm.c\u306eswitchuvm\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Switch TSS and h\/w page table to correspond to process p.\nvoid\nswitchuvm(struct proc *p)\n{\n  pushcli();\n  cpu-&gt;gdt&#x5B;SEG_TSS] = SEG16(STS_T32A, &amp;cpu-&gt;ts, sizeof(cpu-&gt;ts)-1, 0);\n  cpu-&gt;gdt&#x5B;SEG_TSS].s = 0;\n  cpu-&gt;ts.ss0 = SEG_KDATA &lt;&lt; 3;\n  cpu-&gt;ts.esp0 = (uint)proc-&gt;kstack + KSTACKSIZE;\n  ltr(SEG_TSS &lt;&lt; 3);\n  if(p-&gt;pgdir == 0)\n    panic(&quot;switchuvm: no pgdir&quot;);\n  lcr3(v2p(p-&gt;pgdir));  \/\/ switch to new address space\n  popcli();\n}<\/pre>\n<p>\u30c8\u30e9\u30c3\u30d7\u304c\u767a\u751f\u3057\u305f\u3068\u304d\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u306f\u4ee5\u4e0b\u306e\u4e8b\u3092\u884c\u3046\u3002<br \/>\n\u3082\u3057\u30d7\u30ed\u30bb\u30c3\u30b5\u304c\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u3067\u5b9f\u884c\u4e2d\u3060\u3063\u305f\u3089\u3001\u30bf\u30b9\u30af\u30bb\u30b0\u30e1\u30f3\u30c8\u30c7\u30a3\u30b9\u30af\u30ea\u30d7\u30bf\u304b\u3089%esp\u3068%ss\u3092\u30ed\u30fc\u30c9\u3057\u3001\u30e6\u30fc\u30b6\u306e%ss\u3068%esp\u3092\u65b0\u3057\u3044\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002<br \/>\n\u3082\u3057\u30d7\u30ed\u30bb\u30c3\u30b5\u304c\u30ab\u30fc\u30cd\u30eb\u30e2\u30fc\u30c9\u3067\u5b9f\u884c\u4e2d\u3060\u3063\u305f\u3089\u3001\u4e0a\u8a18\u306e\u3088\u3046\u306a\u4e8b\u306f\u4f55\u3082\u884c\u308f\u306a\u3044\u3002<br \/>\n\u305d\u3057\u305f\u3089\u30d7\u30ed\u30bb\u30c3\u30b5\u306f%eflags\u3068%cs\u3068%eip\u30ec\u30b8\u30b9\u30bf\u3092\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002<br \/>\n\u3044\u304f\u3064\u304b\u306e\u30c8\u30e9\u30c3\u30d7\u3067\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u30a8\u30e9\u30fc\u8a9e\uff08error word\uff09\u3082\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002<br \/>\n\u305d\u3057\u305f\u3089\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u9069\u5207\u306aIDT\u30a8\u30f3\u30c8\u30ea\u304b\u3089%eip\u3068%cs\u3092\u8aad\u307f\u8fbc\u3080\u3002<\/p>\n<p>xv6\u306f\u3001IDT\u30a8\u30f3\u30c8\u30ea\u30dd\u30a4\u30f3\u30c8\u3078\u306e\u30a8\u30f3\u30c8\u30ea\u30dd\u30a4\u30f3\u30c8\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u306bPerl\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u4f7f\u3046\u3002<br \/>\n\u305d\u308c\u305e\u308c\u306e\u30a8\u30f3\u30c8\u30ea\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u304c\u3084\u3089\u306a\u304b\u3063\u305f\u3089\u30a8\u30e9\u30fc\u30b3\u30fc\u30c9\u3092\u30d7\u30c3\u30b7\u30e5\u3057\u3001\u5272\u308a\u8fbc\u307f\u756a\u53f7\u3092\u30d7\u30c3\u30b7\u30e5\u3057\u3001\u305d\u3057\u3066alltraps\u306b\u30b8\u30e3\u30f3\u30d7\u3059\u308b\u3002<\/p>\n<p>vectors.pl<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">#!\/usr\/bin\/perl -w\n\n# Generate vectors.S, the trap\/interrupt entry points.\n# There has to be one entry point per interrupt number\n# since otherwise there&#039;s no way for trap() to discover\n# the interrupt number.\n\nprint &quot;# generated by vectors.pl - do not edit\\n&quot;;\nprint &quot;# handlers\\n&quot;;\nprint &quot;.globl alltraps\\n&quot;;\nfor(my $i = 0; $i &lt; 256; $i++){\n    print &quot;.globl vector$i\\n&quot;;\n    print &quot;vector$i:\\n&quot;;\n    if(!($i == 8 || ($i &gt;= 10 &amp;&amp; $i &lt;= 14) || $i == 17)){\n        print &quot;  pushl \\$0\\n&quot;;\n    }\n    print &quot;  pushl \\$$i\\n&quot;;\n    print &quot;  jmp alltraps\\n&quot;;\n}\n\nprint &quot;\\n# vector table\\n&quot;;\nprint &quot;.data\\n&quot;;\nprint &quot;.globl vectors\\n&quot;;\nprint &quot;vectors:\\n&quot;;\nfor(my $i = 0; $i &lt; 256; $i++){\n    print &quot;  .long vector$i\\n&quot;;\n}\n\n# sample output:\n#   # handlers\n#   .globl alltraps\n#   .globl vector0\n#   vector0:\n#     pushl $0\n#     pushl $0\n#     jmp alltraps\n#   ...\n#   \n#   # vector table\n#   .data\n#   .globl vectors\n#   vectors:\n#     .long vector0\n#     .long vector1\n#     .long vector2\n#   ...&amp;#91;\/sourcecode&amp;#93;\n\nalltraps\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u30ec\u30b8\u30b9\u30bf\u306e\u4fdd\u5b58\u3092\u7d9a\u3051\u308b\u3002\n%ds, %es, %fs, %gs\u3001\u305d\u308c\u3068\u6c4e\u7528\u7684\u306a\u30ec\u30b8\u30b9\u30bf\u3092\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002\uff08alltraps\u306epushl %ds\u304b\u3089pushal\u306e\u90e8\u5206\uff09\n\u3053\u306e\u52aa\u529b\u306e\u7d50\u679c\u3001\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306b\u3001\u30c8\u30e9\u30c3\u30d7\u306e\u77ac\u9593\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u30ec\u30b8\u30b9\u30bf\u3092\u542b\u3080trapframe\u69cb\u9020\u4f53\u304c\u542b\u307e\u308c\u308b\u3002\uff08\u56f32\u22122\u3092\u53c2\u7167\uff09\n\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u3001%ss, %esp, %eflags, %cs, %eip\u3092\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002\n\u30d7\u30ed\u30bb\u30c3\u30b5\u3082\u3057\u304f\u306f\u30c8\u30e9\u30c3\u30d7\u30d9\u30af\u30bf\u306f\u30a8\u30e9\u30fc\u756a\u53f7\u3092\u30d7\u30c3\u30b7\u30e5\u3057\u3001alltraps\u306f\u6b8b\u308a\u3092\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002\n\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u306f\u3001\u30ab\u30fc\u30cd\u30eb\u304b\u3089\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u623b\u3063\u305f\u3068\u304d\u306b\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u30ec\u30b8\u30b9\u30bf\u3092\u5fa9\u5143\u3059\u308b\u306e\u306b\u5341\u5206\u306a\u5168\u3066\u306e\u60c5\u5831\u3092\u542b\u3080\u3002\n\u305d\u306e\u7d50\u679c\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u30c8\u30e9\u30c3\u30d7\u304c\u958b\u59cb\u3057\u305f\u6642\u70b9\u3068\u3061\u3087\u3046\u3069\u540c\u3058\u72b6\u614b\u3067\u7d9a\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002\n\u7b2c1\u7ae0\u3092\u601d\u3044\u51fa\u3059\u3068\u3001userinit\u306f\u3053\u306e\u30b4\u30fc\u30eb\u3092\u9054\u6210\u3059\u308b\u305f\u3081\u306b\u3001\u624b\u52d5\u3067\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u3092\u69cb\u7bc9\u3057\u3066\u3044\u308b\u3002\n\uff08\u56f31-3\u53c2\u7167\uff09\n\n\u56f32-2 \u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u4e0a\u306e\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\n&lt;a href=&quot;https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-03-01-7.47.48.png&quot;&gt;&lt;img src=&quot;https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-03-01-7.47.48-271x300.png&quot; alt=&quot;&quot; title=&quot;\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2012-03-01 7.47.48&quot; width=&quot;271&quot; height=&quot;300&quot; class=&quot;aligncenter size-medium wp-image-1384&quot; \/&gt;&lt;\/a&gt;\n\n\u56f31-3 \u65b0\u3057\u3044\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\uff08\u518d\u63b2\uff09\n&lt;a href=&quot;https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-02-19-9.26.23.png&quot;&gt;&lt;img src=&quot;https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/02\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-02-19-9.26.23-294x300.png&quot; alt=&quot;&quot; title=&quot;\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2012-02-19 9.26.23&quot; width=&quot;294&quot; height=&quot;300&quot; class=&quot;aligncenter size-medium wp-image-1288&quot; \/&gt;&lt;\/a&gt;\n\ntrapasm.S\n&#x5B;sourcecode]#include &quot;mmu.h&quot;\n\n  # vectors.S sends all traps here.\n.globl alltraps\nalltraps:\n  # Build trap frame.\n  pushl %ds\n  pushl %es\n  pushl %fs\n  pushl %gs\n  pushal\n  \n  # Set up data and per-cpu segments.\n  movw $(SEG_KDATA&lt;&lt;3), %ax\n  movw %ax, %ds\n  movw %ax, %es\n  movw $(SEG_KCPU&lt;&lt;3), %ax\n  movw %ax, %fs\n  movw %ax, %gs\n\n  # Call trap(tf), where tf=%esp\n  pushl %esp\n  call trap\n  addl $4, %esp\n\n  # Return falls through to trapret...\n.globl trapret\ntrapret:\n  popal\n  popl %gs\n  popl %fs\n  popl %es\n  popl %ds\n  addl $0x8, %esp  # trapno and errcode\n  iret&amp;#91;\/sourcecode&amp;#93;\n\nx86.h\u306etrapframe\u69cb\u9020\u4f53\n&amp;#91;sourcecode language=&quot;c&quot;&amp;#93;\/\/ Layout of the trap frame built on the stack by the\n\/\/ hardware and by trapasm.S, and passed to trap().\nstruct trapframe {\n  \/\/ registers as pushed by pusha\n  uint edi;\n  uint esi;\n  uint ebp;\n  uint oesp;      \/\/ useless &amp; ignored\n  uint ebx;\n  uint edx;\n  uint ecx;\n  uint eax;\n\n  \/\/ rest of trap frame\n  ushort gs;\n  ushort padding1;\n  ushort fs;\n  ushort padding2;\n  ushort es;\n  ushort padding3;\n  ushort ds;\n  ushort padding4;\n  uint trapno;\n\n  \/\/ below here defined by x86 hardware\n  uint err;\n  uint eip;\n  ushort cs;\n  ushort padding5;\n  uint eflags;\n\n  \/\/ below here only when crossing rings, such as from user to kernel\n  uint esp;\n  ushort ss;\n  ushort padding6;\n};&amp;#91;\/sourcecode&amp;#93;\n\n\u6700\u521d\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5834\u5408\u3001\u4fdd\u5b58\u3055\u308c\u305f%eip\u306f\u3001int\u547d\u4ee4\u306e\u76f4\u5f8c\u306e\u547d\u4ee4\u306e\u30a2\u30c9\u30ec\u30b9\u3067\u3042\u308b\u3002\n%cs\u306f\u30e6\u30fc\u30b6\u30b3\u30fc\u30c9\u306e\u30bb\u30b0\u30e1\u30f3\u30c8\u30bb\u30ec\u30af\u30bf\u3067\u3042\u308b\u3002\n%eflags\u306fint\u547d\u4ee4\u3092\u5b9f\u884c\u3057\u305f\u77ac\u9593\u306eeflags\u30ec\u30b8\u30b9\u30bf\u306e\u5185\u5bb9\u3067\u3042\u308b\u3002\n\u4fdd\u5b58\u3055\u308c\u305f\u6c4e\u7528\u30ec\u30b8\u30b9\u30bf\u306e\u4e00\u90e8\u306e\u3088\u3046\u306b\u3001alltraps\u306f\u3001\u30ab\u30fc\u30cd\u30eb\u304c\u5f8c\u3067\u8abf\u3079\u308b\u305f\u3081\u306e\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u756a\u53f7\u3092\u542b\u3080%eax\u3082\u307e\u305f\u4fdd\u5b58\u3059\u308b\u3002\n\n\u4eca\u3001\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u30ec\u30b8\u30b9\u30bf\u306f\u4fdd\u5b58\u3055\u308c\u3001alltraps\u306f\u30d7\u30ed\u30bb\u30c3\u30b5\u306b\u30ab\u30fc\u30cd\u30eb\u306eC\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3055\u305b\u308b\u305f\u3081\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3092\u5b8c\u4e86\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002\n\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u3001\u30cf\u30f3\u30c9\u30e9\u306b\u5165\u308b\u524d\u306b\u30bb\u30ec\u30af\u30bf%cs, %ss\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002\nalltraps\u306f%ds\u3068%es\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002\uff08alltraps\u306emovw $(SEG_KDATA&lt;&lt;3), %ax\u304b\u3089movw %ax, %es\u306e\u90e8\u5206\uff09\n\u305d\u308c\u304b\u3089\u3001CPU\u3054\u3068\u306e\u30c7\u30fc\u30bf\u30bb\u30b0\u30e1\u30f3\u30c8\u3067\u3042\u308bSEG_KCPU\u3092\u6307\u3059\u305f\u3081\u306b%fs\u3068%gs\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002\uff08alltraps\u306emovw $(SEG_KCPU&lt;&lt;3), %ax\u304b\u3089movw %ax, %gs\u306e\u90e8\u5206\uff09\n\n\u4e00\u5ea6\u30bb\u30b0\u30e1\u30f3\u30c8\u304c\u9069\u5207\u306b\u30bb\u30c3\u30c8\u3055\u308c\u305f\u3089\u3001alltraps\u306f\u3001C\u3067\u66f8\u304b\u308c\u305f\u30c8\u30e9\u30c3\u30d7\u30cf\u30f3\u30c9\u30e9\u3067\u3042\u308btrap\u95a2\u6570\uff08trap.c\uff09\u3092\u547c\u3076\u3053\u3068\u304c\u51fa\u6765\u308b\u3002\nalltraps\u306f\u3001\u69cb\u7bc9\u3055\u308c\u305f\u3070\u304b\u308a\u306e\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u3092\u6307\u3059%esp\u3092\u3001trap\u3078\u306e\u5f15\u6570\u3068\u3057\u3066\u30b9\u30bf\u30c3\u30af\u306b\u30d7\u30c3\u30b7\u30e5\u3059\u308b\u3002\uff08alltraps\u306epushl %esp\u306e\u90e8\u5206\uff09\n\u305d\u3057\u3066trap\u3092\u547c\u3076\u3002\uff08alltraps\u306ecall trap\u306e\u90e8\u5206\uff09\ntrap\u95a2\u6570\u304b\u3089\u8fd4\u3063\u3066\u304d\u305f\u5f8c\u3001alltraps\u306f\u3001\u30b9\u30bf\u30c3\u30af\u30dd\u30a4\u30f3\u30bf\u306b\u52a0\u7b97\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u30b9\u30bf\u30c3\u30af\u306e\u5f15\u6570\u3092\u53d6\u308a\u9664\u304d\u3001\u305d\u3057\u3066\u3001trapret\u3068\u30e9\u30d9\u30eb\u3065\u3051\u3055\u308c\u305f\u30b3\u30fc\u30c9\u306e\u5b9f\u884c\u3092\u958b\u59cb\u3059\u308b\u3002\n\u6211\u3005\u306f\u3001\u7b2c1\u7ae0\u3067\u3001\u6700\u521d\u306e\u30e6\u30fc\u30b6\u30d7\u30ed\u30bb\u30b9\u304c\u30e6\u30fc\u30b6\u7a7a\u9593\u306b\u629c\u3051\u308b\u305f\u3081\u306btrapret\u3092\u5b9f\u884c\u3057\u305f\u3068\u304d\u3001\u3053\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u3063\u305f\u3002\n\u540c\u3058\u51e6\u7406\u304c\u3053\u3053\u3067\u8d77\u304d\u305f\u3002\n\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u3092\u901a\u3058\u3066\u30dd\u30c3\u30d7\u3059\u308b\u4e8b\u306f\u3001\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u306e\u30ec\u30b8\u30b9\u30bf\u3092\u5fa9\u5143\u3057\u3001\u305d\u3057\u3066iret\u306f\u30e6\u30fc\u30b6\u7a7a\u9593\u306b\u623b\u3059\u3002\n\ntrap.c\u306etrap\u95a2\u6570\n&amp;#91;sourcecode language=&quot;c&quot;&amp;#93;void\ntrap(struct trapframe *tf)\n{\n  if(tf-&gt;trapno == T_SYSCALL){\n    if(proc-&gt;killed)\n      exit();\n    proc-&gt;tf = tf;\n    syscall();\n    if(proc-&gt;killed)\n      exit();\n    return;\n  }\n\n  switch(tf-&gt;trapno){\n  case T_IRQ0 + IRQ_TIMER:\n    if(cpu-&gt;id == 0){\n      acquire(&amp;tickslock);\n      ticks++;\n      wakeup(&amp;ticks);\n      release(&amp;tickslock);\n    }\n    lapiceoi();\n    break;\n  case T_IRQ0 + IRQ_IDE:\n    ideintr();\n    lapiceoi();\n    break;\n  case T_IRQ0 + IRQ_IDE+1:\n    \/\/ Bochs generates spurious IDE1 interrupts.\n    break;\n  case T_IRQ0 + IRQ_KBD:\n    kbdintr();\n    lapiceoi();\n    break;\n  case T_IRQ0 + IRQ_COM1:\n    uartintr();\n    lapiceoi();\n    break;\n  case T_IRQ0 + 7:\n  case T_IRQ0 + IRQ_SPURIOUS:\n    cprintf(&quot;cpu%d: spurious interrupt at %x:%x\\n&quot;,\n            cpu-&gt;id, tf-&gt;cs, tf-&gt;eip);\n    lapiceoi();\n    break;\n   \n  \/\/PAGEBREAK: 13\n  default:\n    if(proc == 0 || (tf-&gt;cs&amp;3) == 0){\n      \/\/ In kernel, it must be our mistake.\n      cprintf(&quot;unexpected trap %d from cpu %d eip %x (cr2=0x%x)\\n&quot;,\n              tf-&gt;trapno, cpu-&gt;id, tf-&gt;eip, rcr2());\n      panic(&quot;trap&quot;);\n    }\n    \/\/ In user space, assume process misbehaved.\n    cprintf(&quot;pid %d %s: trap %d err %d on cpu %d &quot;\n            &quot;eip 0x%x addr 0x%x--kill proc\\n&quot;,\n            proc-&gt;pid, proc-&gt;name, tf-&gt;trapno, tf-&gt;err, cpu-&gt;id, tf-&gt;eip, \n            rcr2());\n    proc-&gt;killed = 1;\n  }\n\n  \/\/ Force process exit if it has been killed and is in user space.\n  \/\/ (If it is still executing in the kernel, let it keep running \n  \/\/ until it gets to the regular system call return.)\n  if(proc &amp;&amp; proc-&gt;killed &amp;&amp; (tf-&gt;cs&amp;3) == DPL_USER)\n    exit();\n\n  \/\/ Force process to give up CPU on clock tick.\n  \/\/ If interrupts were on while locks held, would need to check nlock.\n  if(proc &amp;&amp; proc-&gt;state == RUNNING &amp;&amp; tf-&gt;trapno == T_IRQ0+IRQ_TIMER)\n    yield();\n\n  \/\/ Check if the process has been killed since we yielded\n  if(proc &amp;&amp; proc-&gt;killed &amp;&amp; (tf-&gt;cs&amp;3) == DPL_USER)\n    exit();\n}<\/pre>\n<p>\u3053\u3053\u307e\u3067\u306e\u89e3\u8aac\u306f\u3001\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u304b\u3089\u767a\u751f\u3057\u305f\u30c8\u30e9\u30c3\u30d7\u306b\u3064\u3044\u3066\u8a71\u3057\u3066\u304d\u305f\u3002<br \/>\n\u3057\u304b\u3057\u30c8\u30e9\u30c3\u30d7\u306f\u30ab\u30fc\u30cd\u30eb\u3092\u5b9f\u884c\u3057\u3066\u308b\u3068\u304d\u3067\u3082\u767a\u751f\u3059\u308b\u3002<br \/>\n\u305d\u306e\u3088\u3046\u306a\u5834\u5408\u3001\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u306f\u30b9\u30bf\u30c3\u30af\u3092\u5207\u308a\u66ff\u3048\u305f\u308a\u3001\u30b9\u30bf\u30c3\u30af\u30dd\u30a4\u30f3\u30bf\u3084\u30b9\u30bf\u30c3\u30af\u30bb\u30b0\u30e1\u30f3\u30c8\u30bb\u30ec\u30af\u30bf\u3092\u4fdd\u5b58\u3057\u305f\u308a\u306f\u3057\u306a\u3044\u3002<br \/>\n\u3057\u304b\u3057\u306a\u304c\u3089\u3001\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u304b\u3089\u306e\u30c8\u30e9\u30c3\u30d7\u3068\u540c\u3058\u624b\u9806\u304c\u767a\u751f\u3057\u3001\u305d\u3057\u3066\u540c\u3058xv6\u306e\u30c8\u30e9\u30c3\u30d7\u3092\u5236\u5fa1\u3059\u308b\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002<br \/>\niret\u306e\u5f8c\u3001\u30ab\u30fc\u30cd\u30eb\u30e2\u30fc\u30c9\u306e%cs\u3092\u5fa9\u5143\u3057\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u30ab\u30fc\u30cd\u30eb\u30e2\u30fc\u30c9\u3067\u306e\u5b9f\u884c\u3092\u7d9a\u3051\u308b\u3002<\/p>\n<h3>\u611f\u60f3<\/h3>\n<p>\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u3067\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u3057\u305f\u3068\u304d\u306b\u767a\u751f\u3059\u308b\u30c8\u30e9\u30c3\u30d7\u3067\u3001\u3069\u3046\u3084\u3063\u3066\u30ab\u30fc\u30cd\u30eb\u30e2\u30fc\u30c9\u306b\u79fb\u884c\u3057\u3066\u3069\u3046\u3084\u3063\u3066\u623b\u308b\u304b\u306e\u8a71\u3067\u3059\u306d\u3002<\/p>\n<p>\u672c\u6587\u306b\u3082\u66f8\u3044\u3066\u3042\u308b\u3051\u3069\u3001vectors.S\u306fvectors.pl\u304b\u3089\u751f\u6210\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u308b\u306e\u3067\u3001\u4e00\u5ea6make\u3068\u304b\u3057\u306a\u3044\u3068\u5b58\u5728\u3057\u307e\u305b\u3093\u3002<\/p>\n<p>\u30bf\u30b9\u30af\u30bb\u30b0\u30e1\u30f3\u30c8\u30c7\u30a3\u30b9\u30af\u30ea\u30d7\u30bf\u5468\u308a\u306e\u8a71\u304c\u3088\u304f\u308f\u304b\u308a\u307e\u305b\u3093\u3002<br \/>\n<a href=\"http:\/\/ja.wikipedia.org\/wiki\/%E3%83%97%E3%83%AD%E3%83%86%E3%82%AF%E3%83%88%E3%83%A2%E3%83%BC%E3%83%89\" target=\"_blank\">\u30d7\u30ed\u30c6\u30af\u30c8\u30e2\u30fc\u30c9 &#8211; Wikipedia<\/a>\u3092\u8aad\u3081\u3070\u5c11\u3057\u306f\u5206\u304b\u308b\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-1383","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\/1383","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=1383"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1383\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=1383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=1383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=1383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}