{"id":1285,"date":"2012-02-19T18:27:01","date_gmt":"2012-02-19T00:27:01","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=1285"},"modified":"2012-03-14T11:22:03","modified_gmt":"2012-03-14T02:22:03","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%ae15","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/1285","title":{"rendered":"[xv6 #15] Chapter 1 &#8211; The first process &#8211; Code: Process creation"},"content":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e23\u301c25\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>\u3053\u306e\u7bc0\u3067\u306f\u3001xv6\u304c\u3069\u3046\u3084\u3063\u3066\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u751f\u6210\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u8ff0\u3079\u308b\u3002<br \/>\nxv6\u306e\u30ab\u30fc\u30cd\u30eb\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u3054\u3068\u306b\u591a\u304f\u306e\u72b6\u614b\u3092\u7ba1\u7406\u3059\u308b\u3002<br \/>\n\u305d\u308c\u3089\u306e\u72b6\u614b\u306fproc\u69cb\u9020\u4f53\u306b\u307e\u3068\u3081\u3089\u308c\u3066\u3044\u308b\u3002<br \/>\n\u3072\u3068\u3064\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u72b6\u614b\u3067\u4e00\u756a\u91cd\u8981\u306a\u90e8\u54c1\u306f\u3001\u305d\u308c\u81ea\u8eab\u306e\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u3068\u305d\u308c\u304c\u6307\u3059\u7269\u7406\u30e1\u30e2\u30ea\u3001\u305d\u308c\u81ea\u8eab\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u3001\u305d\u308c\u81ea\u8eab\u306e\u5b9f\u884c\u72b6\u614b\uff08run state\uff09\u3067\u3042\u308b\u3002<br \/>\nproc\u69cb\u9020\u4f53\u306e\u8981\u7d20\u3092\u6307\u3059\u305f\u3081\u306e\u8a18\u6cd5\u3068\u3057\u3066\u3001p-&gt;xxx\u3068\u3044\u3046\u5f62\u3092\u7528\u3044\u308b\u3002<\/p>\n<p>proc.h\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u308bproc\u69cb\u9020\u4f53<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Per-process state\nstruct proc {\n  uint sz;                     \/\/ Size of process memory (bytes)\n  pde_t* pgdir;                \/\/ Page table\n  char *kstack;                \/\/ Bottom of kernel stack for this process\n  enum procstate state;        \/\/ Process state\n  volatile int pid;            \/\/ Process ID\n  struct proc *parent;         \/\/ Parent process\n  struct trapframe *tf;        \/\/ Trap frame for current syscall\n  struct context *context;     \/\/ swtch() here to run process\n  void *chan;                  \/\/ If non-zero, sleeping on chan\n  int killed;                  \/\/ If non-zero, have been killed\n  struct file *ofile&#x5B;NOFILE];  \/\/ Open files\n  struct inode *cwd;           \/\/ Current directory\n  char name&#x5B;16];               \/\/ Process name (debugging)\n};<\/pre>\n<p>\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u72b6\u614b\u3092\u3001\u30d7\u30ed\u30bb\u30b9\u306e\u5473\u65b9\u4e0a\u306e\u30ab\u30fc\u30cd\u30eb\u306e\u4e2d\u3067\u5b9f\u884c\u3055\u308c\u308b\u3001\u5b9f\u884c\u30b9\u30ec\u30c3\u30c9\uff08or thread for short\uff09\u3068\u3057\u3066\u898b\u308b\u3079\u304d\u3067\u3042\u308b\u3002<br \/>\n\uff08\u591a\u5206\u3001\u300c\u30d7\u30ed\u30bb\u30b9\u306e\u5473\u65b9\u4e0a\u306e\u30ab\u30fc\u30cd\u30eb\u300d=\u300c\u30d7\u30ed\u30bb\u30b9\u306e\u4eee\u60f3\u30a2\u30c9\u30ec\u30b9\u7a7a\u9593\u4e0a\u306b\u914d\u7f6e\u3055\u308c\u305f\u30ab\u30fc\u30cd\u30eb\u300d\u3002\u300cor thread for short\u300d\u306e\u65b9\u306f\u3046\u307e\u3044\u8a33\u304c\u601d\u3044\u3064\u304b\u306a\u3044\u3069\u3053\u308d\u304b\u30a4\u30e1\u30fc\u30b8\u3082\u8b0e\uff09<br \/>\n\u30b9\u30ec\u30c3\u30c9\u306f\u3001\u8a08\u7b97\u3092\u5b9f\u884c\u3059\u308b\u3002<br \/>\n\u3057\u304b\u3057\u305d\u308c\u306f\u6b62\u3081\u308c\u308b\u3057\u518d\u958b\u3082\u51fa\u6765\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u30d7\u30ed\u30bb\u30b9\u304c\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u3092\u547c\u3076\u3068\u304d\u3001CPU\u306f\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u5b9f\u884c\u304b\u3089\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u306e\u5b9f\u884c\u3078\u5207\u308a\u66ff\u3048\u308b\u3002<br \/>\n\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5b9f\u88c5\uff08\u4f8b\u3048\u3070\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u3093\u3060\u308a\uff09\u3092\u5b9f\u884c\u3059\u308b\u3002<br \/>\n\u305d\u3057\u3066\u305d\u308c\u304b\u3089\u30d7\u30ed\u30bb\u30b9\u306b\u623b\u308b\u3002<br \/>\nxv6\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u304c\u30ab\u30fc\u30cd\u30eb\u306e\u4e2d\u3067I\/O\u306e\u305f\u3081\u306b\u5f85\u3066\u308b\uff08\u3082\u3057\u304f\u306f&#8221;block&#8221;\u3067\u304d\u308b\uff09\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u3001\u305d\u3057\u3066I\/O\u304c\u7d42\u308f\u3063\u305f\u3068\u304d\u306b\u5143\u306e\u5b9f\u884c\u4e2d\u306e\u5834\u6240\u306b\u5fa9\u5e30\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u30b9\u30ec\u30c3\u30c9\u3068\u3057\u3066\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u3092\u5b9f\u884c\u3059\u308b\u3002<br \/>\n\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u306e\u591a\u304f\u306e\u72b6\u614b\uff08\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3001\u95a2\u6570\u306e\u623b\u308a\u5148\u30a2\u30c9\u30ec\u30b9\uff09\u306f\u3001\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u306e\u30b9\u30bf\u30c3\u30afp-&gt;kstack\u306b\u914d\u7f6e\u3055\u308c\u308b\u3002<br \/>\n\u305d\u308c\u305e\u308c\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306f\u3001\u30e6\u30fc\u30b6\u30b9\u30bf\u30c3\u30af\u3092\u58ca\u3055\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u30e6\u30fc\u30b6\u30b9\u30bf\u30c3\u30af\u3068\u306f\u5225\u306b\u306a\u3063\u3066\u3044\u308b\u3002<br \/>\n\u3064\u307e\u308a\u30d7\u30ed\u30bb\u30b9\u3092\u30012\u3064\u306e\u5b9f\u884c\u30b9\u30ec\u30c3\u30c9\u3092\u6301\u3063\u3066\u3044\u308b\u3068\u307f\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br \/>\n\u4e00\u3064\u306f\u30e6\u30fc\u30b6\u30b9\u30ec\u30c3\u30c9\u3001\u3082\u3046\u4e00\u3064\u306f\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u3067\u3042\u308b\u3002<\/p>\n<p>p-&gt;state\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u304c\u5272\u308a\u5f53\u3066\u6e08\u307f\u304b\u3001\u5b9f\u884c\u6e96\u5099\u304c\u5b8c\u4e86\u3057\u3066\u308b\u304b\u3001\u5b9f\u884c\u4e2d\u304b\u3001I\/O\u5f85\u3061\u304b\u3001\u7d42\u4e86\u4e2d\u304b\u3069\u3046\u304b\u3092\u793a\u3059\u3002<\/p>\n<p>p-&gt;pgdir\u306f\u3001PTE\u306e\u914d\u5217\u3092\u4fdd\u6301\u3059\u308b\u3002<br \/>\nxv6\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u304c\u5b9f\u884c\u3055\u308c\u308b\u3068\u304d\u3001\u30da\u30fc\u30b8\u30f3\u30b0\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u306b\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306ep-&gt;pgdir\u3092\u4f7f\u308f\u305b\u308b\u3088\u3046\u306b\u3059\u308b\u3002<br \/>\n\u30d7\u30ed\u30bb\u30b9\u306e\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u306f\u307e\u305f\u3001\u30d7\u30ed\u30bb\u30b9\u306e\u30e1\u30e2\u30ea\u3092\u4fdd\u6301\u3059\u308b\u305f\u3081\u306b\u5272\u308a\u5f53\u3066\u6e08\u307f\u306e\u7269\u7406\u30da\u30fc\u30b8\u306e\u30a2\u30c9\u30ec\u30b9\u306e\u30ec\u30b3\u30fc\u30c9\u3068\u3057\u3066\u52d5\u304f\u3002<\/p>\n<p>\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u751f\u6210\u3059\u308b\u6d41\u308c\u306f\u3001main\u95a2\u6570\u304cuserinit\u95a2\u6570\u3092\u547c\u3076\u3068\u3053\u308d\u304b\u3089\u59cb\u307e\u308b\u3002<br \/>\nuserinit\u95a2\u6570\u306e\u6700\u521d\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u306fallocproc\u95a2\u6570\u3092\u547c\u3076\u3053\u3068\u3067\u3042\u308b\u3002<br \/>\nallocproc\u306e\u4ed5\u4e8b\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u30c6\u30fc\u30d6\u30eb\u306b\u30b9\u30ed\u30c3\u30c8\uff08proc\u69cb\u9020\u4f53\uff09\u3092\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u3068\u3001\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u30d7\u30ed\u30bb\u30b9\u72b6\u614b\u306e\u4e00\u90e8\u306e\u521d\u671f\u5316\u3067\u3042\u308b\u3002<br \/>\nuserinit\u304c\u4e00\u756a\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u305f\u3081\u306b\u547c\u3070\u308c\u308b\u9593\u3001allocproc\u306f\u3001\u5168\u3066\u306e\u65b0\u3057\u3044\u30d7\u30ed\u30bb\u30b9\u306e\u305f\u3081\u306b\u547c\u3070\u308c\u308b\u3002<br \/>\nallocproc\u306f\u3001UNUSED\u72b6\u614b\u3092\u30ad\u30fc\u306b\u30d7\u30ed\u30bb\u30b9\u30c6\u30fc\u30d6\u30eb\u3092\u30b9\u30ad\u30e3\u30f3\u3059\u308b\u3002<br \/>\n\u672a\u4f7f\u7528\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u898b\u3064\u3051\u305f\u3068\u304d\u3001allocproc\u306f\u4f7f\u7528\u4e2d\u3092\u793a\u3059\u305f\u3081EMBRYO\u3092\u30bb\u30c3\u30c8\u3057\u3001\u4e00\u610f\u306epid\u3092\u4ed8\u4e0e\u3059\u308b\u3002<br \/>\n\u6b21\u306b\u3001\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u306e\u305f\u3081\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u3092\u5272\u308a\u5f53\u3066\u3088\u3046\u3068\u3059\u308b\u3002<br \/>\n\u3082\u3057\u5272\u308a\u5f53\u3066\u304c\u5931\u6557\u3057\u305f\u3089\u3001allocproc\u306f\u72b6\u614b\u3092UNUSED\u306b\u623b\u3057\u3001\u5931\u6557\u3092\u4f1d\u3048\u308b\u305f\u3081\u306b\u30bc\u30ed\u3092\u8fd4\u3059\u3002<\/p>\n<p>main.c\u306emain\u95a2\u6570\uff08\u53c2\u7167\u30b3\u30df\u30c3\u30c8\u304c\u5909\u308f\u3063\u305f\u306e\u3067\u4ee5\u524d\u8f09\u305b\u305f\u3082\u306e\u3068\u306f\u9055\u3046\u306f\u305a\u3067\u3059\u3002\uff09<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ 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>proc.c\u306euserinit\u95a2\u6570\u3068allocproc\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/PAGEBREAK: 32\n\/\/ Set up first user process.\nvoid\nuserinit(void)\n{\n  struct proc *p;\n  extern char _binary_initcode_start&#x5B;], _binary_initcode_size&#x5B;];\n  \n  p = allocproc();\n  initproc = p;\n  if((p-&gt;pgdir = setupkvm(kalloc)) == 0)\n    panic(&quot;userinit: out of memory?&quot;);\n  inituvm(p-&gt;pgdir, _binary_initcode_start, (int)_binary_initcode_size);\n  p-&gt;sz = PGSIZE;\n  memset(p-&gt;tf, 0, sizeof(*p-&gt;tf));\n  p-&gt;tf-&gt;cs = (SEG_UCODE &lt;&lt; 3) | DPL_USER;\n  p-&gt;tf-&gt;ds = (SEG_UDATA &lt;&lt; 3) | DPL_USER;\n  p-&gt;tf-&gt;es = p-&gt;tf-&gt;ds;\n  p-&gt;tf-&gt;ss = p-&gt;tf-&gt;ds;\n  p-&gt;tf-&gt;eflags = FL_IF;\n  p-&gt;tf-&gt;esp = PGSIZE;\n  p-&gt;tf-&gt;eip = 0;  \/\/ beginning of initcode.S\n\n  safestrcpy(p-&gt;name, &quot;initcode&quot;, sizeof(p-&gt;name));\n  p-&gt;cwd = namei(&quot;\/&quot;);\n\n  p-&gt;state = RUNNABLE;\n}\n\n\/\/PAGEBREAK: 32\n\/\/ Look in the process table for an UNUSED proc.\n\/\/ If found, change state to EMBRYO and initialize\n\/\/ state required to run in the kernel.\n\/\/ Otherwise return 0.\nstatic struct proc*\nallocproc(void)\n{\n  struct proc *p;\n  char *sp;\n\n  acquire(&amp;ptable.lock);\n  for(p = ptable.proc; p &lt; &amp;ptable.proc&amp;#91;NPROC&amp;#93;; p++)\n    if(p-&gt;state == UNUSED)\n      goto found;\n  release(&amp;ptable.lock);\n  return 0;\n\nfound:\n  p-&gt;state = EMBRYO;\n  p-&gt;pid = nextpid++;\n  release(&amp;ptable.lock);\n\n  \/\/ Allocate kernel stack.\n  if((p-&gt;kstack = kalloc()) == 0){\n    p-&gt;state = UNUSED;\n    return 0;\n  }\n  sp = p-&gt;kstack + KSTACKSIZE;\n  \n  \/\/ Leave room for trap frame.\n  sp -= sizeof *p-&gt;tf;\n  p-&gt;tf = (struct trapframe*)sp;\n  \n  \/\/ Set up new context to start executing at forkret,\n  \/\/ which returns to trapret.\n  sp -= 4;\n  *(uint*)sp = (uint)trapret;\n\n  sp -= sizeof *p-&gt;context;\n  p-&gt;context = (struct context*)sp;\n  memset(p-&gt;context, 0, sizeof *p-&gt;context);\n  p-&gt;context-&gt;eip = (uint)forkret;\n\n  return p;\n}<\/pre>\n<p>\u3055\u3066\u3053\u3053\u3067\u3001allocproc\u306f\u65b0\u3057\u3044\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\n\u901a\u5e38\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u306ffork\u306b\u3088\u3063\u3066\u306e\u307f\u4f5c\u3089\u308c\u308b\u3002<br \/>\n\u306a\u306e\u3067\u3001\u65b0\u3057\u3044\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u89aa\u304b\u3089\u30b3\u30d4\u30fc\u3055\u308c\u3066\u59cb\u307e\u308b\u3002<br \/>\nfork\u306e\u7d50\u679c\u3001\u5185\u5bb9\u306f\u89aa\u3068\u540c\u3058\u3060\u304c\u72ec\u7acb\u306a\u30e1\u30e2\u30ea\u3092\u6301\u3064\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u751f\u307e\u308c\u308b\u3002<br \/>\nallocproc\u306f\u3001\u7279\u5225\u306b\u7528\u610f\u3055\u308c\u305f\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u3068\u3001\u89aa\u3068\u540c\u3058\u30e6\u30fc\u30b6\u7a7a\u9593\u4e0a\u306e\u5834\u6240\uff08fork\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u304b\u3089\u623b\u308b\u5834\u6240\uff09\u306b\u623b\u308b\u305f\u3081\u306e\u30ab\u30fc\u30cd\u30eb\u30ec\u30b8\u30b9\u30bf\u306e\u7d44\u3067\u3001\u305d\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u3067\u306e\u5b9f\u884c\u3092\u958b\u59cb\u3059\u308b\u305f\u3081\u5b50\u30d7\u30ed\u30bb\u30b9\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u3002<br \/>\n\u305d\u306e\u7528\u610f\u3055\u308c\u305f\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306e\u30ec\u30a4\u30a2\u30a6\u30c8\u306f\u56f31-3\u306b\u793a\u3059\u3002<br \/>\nallocproc\u306f\u3053\u306e\u4ed5\u4e8b\u306e\u4e00\u90e8\u3092\u3001\u30ea\u30bf\u30fc\u30f3\u30d7\u30ed\u30b0\u30e9\u30e0\u30ab\u30a6\u30f3\u30bf\u5024\uff08return program counter values\uff09\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u3053\u3068\u3067\u3053\u306a\u3059\u3002<br \/>\n\u30ea\u30bf\u30fc\u30f3\u30d7\u30ed\u30b0\u30e9\u30e0\u30ab\u30a6\u30f3\u30bf\u5024\u306f\u3001\u65b0\u3057\u3044\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u3092\u307e\u305aforkret\u3067\u5b9f\u884c\u3057\u305d\u3057\u3066trapret\u3067\u5b9f\u884c\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u3002\uff08allocproc\u95a2\u6570\u306e\u6700\u5f8c\u306e\u3042\u305f\u308a\u3092\u53c2\u7167\u3002\u30b3\u30fc\u30c9\u4e0a\u3067\u306f\u307e\u305a\u623b\u308a\u5148\u306etrapret\u3092\u8a2d\u5b9a\u3057\u3066\u304b\u3089\u98db\u3073\u5148\u306eforkret\u3092\u8a2d\u5b9a\u3057\u3066\u308b\u3002\uff09<br \/>\n\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u306f\u3001p-&gt;context\u304b\u3089\u30b3\u30d4\u30fc\u3055\u308c\u305f\u30ec\u30b8\u30b9\u30bf\u306e\u5185\u5bb9\u3067\u5b9f\u884c\u3092\u958b\u59cb\u3059\u308b\u3002<br \/>\n\u8981\u3059\u308b\u306b\u3001p-&gt;context-&gt;eip\u3092forkret\u306b\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u306f\u3001\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u3092forkret\u306e\u306f\u3058\u3081\u304b\u3089\u5b9f\u884c\u3059\u308b\u3053\u3068\u3092\u5f15\u304d\u8d77\u3053\u3059\u3002\uff08\u5f8c\u306b\u8f09\u305b\u307e\u3059\u304c\u3001forkret\u306f\u95a2\u6570\u3067\u3059\u3002\u3064\u307e\u308a\u95a2\u6570\u30dd\u30a4\u30f3\u30bf\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\uff09<br \/>\n\u3053\u306e\u95a2\u6570\u306f\u3001\u30b9\u30bf\u30c3\u30af\u306e\u4e0b\u306b\u3042\u308b\u30a2\u30c9\u30ec\u30b9\u306b\u3068\u306b\u304b\u304f\u623b\u308b\u3002<br \/>\n\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30b9\u30a4\u30c3\u30c1\u306e\u30b3\u30fc\u30c9\uff08\u4ee5\u4e0b\u306eswitch.S\u53c2\u7167\uff09\u306f\u3001p-&gt;context\u306e\u7d42\u308f\u308a\u306e\u3059\u3050\u6b21\u3092\u6307\u3059\u3088\u3046\u306b\u30b9\u30bf\u30c3\u30af\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002<br \/>\nallocproc\u306f\u3001\u30b9\u30bf\u30c3\u30af\u4e0a\u306bp-&gt;context\u3092\u7f6e\u304d\u3001\u305d\u3057\u3066trapret\u3078\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u3059\u3050\u305d\u306e\u4e0a\u306b\u7f6e\u304f\u3002<br \/>\n\u305d\u308c\u304cforkret\u306e\u623b\u308a\u5148\u3068\u306a\u308b\u3002<br \/>\ntrapret\u306f\u3001\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u306b\u4fdd\u6301\u3055\u308c\u305f\u5024\u304b\u3089\u30e6\u30fc\u30b6\u30ec\u30b8\u30b9\u30bf\u3092\u5fa9\u5143\u3057\u3001\u30d7\u30ed\u30bb\u30b9\u306b\u30b8\u30e3\u30f3\u30d7\u3059\u308b\u3002\uff08\u4ee5\u4e0b\u306etrapasm.S\u53c2\u7167\uff09<br \/>\n\u3053\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u306f\u3001\u901a\u5e38\u306efork\u3084\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u751f\u6210\u3068\u540c\u3058\u3067\u3042\u308b\u3002<br \/>\n\u305f\u3060\u3001\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u751f\u6210\u306e\u5834\u5408\u306f\u3001fork\u304b\u3089\u8fd4\u3063\u3066\u304d\u305f\u3068\u3053\u308d\u304b\u3089\u3068\u3044\u3046\u3088\u308a\u3080\u3057\u308d\u3001\u4f4d\u7f6e\u30bc\u30ed\u304b\u3089\u5b9f\u884c\u3092\u59cb\u3081\u308b\u3002<\/p>\n<p>\u56f31-3 \u65b0\u3057\u3044\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7<br \/>\n<a href=\"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\"><img loading=\"lazy\" decoding=\"async\" src=\"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\" alt=\"\" title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2012-02-19 9.26.23\" width=\"474\" height=\"483\" class=\"aligncenter size-full wp-image-1288\" srcset=\"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 474w, 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 294w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<p>proc.c\u306eforkret\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ A fork child's very first scheduling by scheduler()\n\/\/ will swtch here.  \"Return\" to user space.\nvoid\nforkret(void)\n{\n  static int first = 1;\n  \/\/ Still holding ptable.lock from scheduler.\n  release(&ptable.lock);\n\n  if (first) {\n    \/\/ Some initialization functions must be run in the context\n    \/\/ of a regular process (e.g., they call sleep), and thus cannot \n    \/\/ be run from main().\n    first = 0;\n    initlog();\n  }\n  \n  \/\/ Return to \"caller\", actually trapret (see allocproc).\n}<\/pre>\n<p>switch.S\u306eswitch\uff08\u3053\u308c\u3067switch.S\u306e\u5168\u3066\u3067\u3059\u3002\uff09<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\"># Context switch\n#\n#   void swtch(struct context **old, struct context *new);\n# \n# Save current register context in old\n# and then load register context from new.\n\n.globl swtch\nswtch:\n  movl 4(%esp), %eax\n  movl 8(%esp), %edx\n\n  # Save old callee-save registers\n  pushl %ebp\n  pushl %ebx\n  pushl %esi\n  pushl %edi\n\n  # Switch stacks\n  movl %esp, (%eax)\n  movl %edx, %esp\n\n  # Load new callee-save registers\n  popl %edi\n  popl %esi\n  popl %ebx\n  popl %ebp\n  ret\n\ntrapasm.S\u306etrapret\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<\/pre>\n<p>\u7b2c2\u7ae0\u306b\u8f09\u3063\u3066\u308b\u304c\u3001\u30e6\u30fc\u30b6\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u304b\u3089\u30ab\u30fc\u30cd\u30eb\u3078\u306e\u5236\u5fa1\u3092\u79fb\u52d5\u3059\u308b\u65b9\u6cd5\u306f\u3001\u5272\u308a\u8fbc\u307f\u306e\u4ed5\u7d44\u307f\u3092\u5143\u306b\u3057\u3066\u3044\u308b\u3002<br \/>\n\u305d\u306e\u4ed5\u7d44\u307f\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u3084\u4e2d\u65ad\u3084\u4f8b\u5916\u3067\u4f7f\u308f\u308c\u308b\u3002<br \/>\n\u30d7\u30ed\u30bb\u30b9\u304c\u5b9f\u884c\u3055\u308c\u3066\u308b\u9593\u3001\u5236\u5fa1\u3092\u30ab\u30fc\u30cd\u30eb\u306b\u79fb\u3059\u3068\u304d\u306f\u3044\u3064\u3067\u3082\u3001\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u3068xv6\u306f\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u306b\u30e6\u30fc\u30b6\u30ec\u30b8\u30b9\u30bf\u3092\u4fdd\u5b58\u3057\u3001\u305d\u306e\u3068\u304d\u3068\u306e\u30b3\u30fc\u30c9\u3092\u30c8\u30e9\u30c3\u30d7\u3059\u308b\u3002<br \/>\nuserinit\u95a2\u6570\u306f\u3001\u3082\u3057\u30d7\u30ed\u30bb\u30b9\u304c\u5272\u308a\u8fbc\u307f\u7d4c\u7531\uff08userinit\u95a2\u6570\u306e\u4e2d\u3067p-&gt;tf\u306e\u4e00\u9023\u306e\u30e1\u30f3\u30d0\u306b\u5024\u3092\u30bb\u30c3\u30c8\u3057\u3066\u308b\u90e8\u5206\uff09\u3067\u30ab\u30fc\u30cd\u30eb\u306b\u5165\u3063\u305f\u3089\u3001\u65b0\u3057\u3044\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u306b\u3001\u305d\u3053\u306b\u3042\u308b\u3067\u3042\u308d\u3046\u5f7c\u3089\u306b\u305d\u3063\u304f\u308a\u306a\u5024\u3092\u66f8\u304d\u8fbc\u3080\u3002<br \/>\n\u3060\u304b\u3089\u3001\u30ab\u30fc\u30cd\u30eb\u304b\u3089\u30d7\u30ed\u30bb\u30b9\u306e\u30e6\u30fc\u30b6\u30b3\u30fc\u30c9\u306b\u623b\u308b\u901a\u5e38\u306e\u30b3\u30fc\u30c9\u306f\u3046\u307e\u304f\u52d5\u4f5c\u3059\u308b\u3002<br \/>\n\u305d\u308c\u3089\u306e\u5024\u306f\u3001\u30e6\u30fc\u30b6\u30ec\u30b8\u30b9\u30bf\u306b\u4fdd\u6301\u3055\u308c\u308btrapframe\u69cb\u9020\u4f53\u3067\u3042\u308b\u3002<br \/>\n\u305d\u3046\u3084\u3063\u3066\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306f\u3001\u56f31-3\u3067\u793a\u3055\u308c\u308b\u3088\u3046\u306a\u6e96\u5099\u5b8c\u4e86\u72b6\u614b\u306b\u306a\u308b\u3002<\/p>\n<p>\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u5c0f\u3055\u3044\u30d7\u30ed\u30b0\u30e9\u30e0\uff08initcode.S\uff09\u3092\u5b9f\u884c\u3059\u308b\u3002<br \/>\n\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u3053\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4fdd\u6301\u3059\u308b\u305f\u3081\u306e\u7269\u7406\u30e1\u30e2\u30ea\u304c\u5fc5\u8981\u3067\u3042\u308b\u3002<br \/>\n\u305d\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001\u305d\u306e\u30e1\u30e2\u30ea\u306b\u30b3\u30d4\u30fc\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u305d\u3057\u3066\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u305d\u306e\u30e1\u30e2\u30ea\u3092\u6307\u3059\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u3092\u5fc5\u8981\u3068\u3059\u308b\u3002<\/p>\n<p>initcode.S\uff08\u3053\u308c\u3067\u5168\u3066\u3067\u3059\u3002\uff09<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\"># Initial process execs \/init.\n\n#include \"syscall.h\"\n#include \"traps.h\"\n\n\n# exec(init, argv)\n.globl start\nstart:\n  pushl $argv\n  pushl $init\n  pushl $0  \/\/ where caller pc would be\n  movl $SYS_exec, %eax\n  int $T_SYSCALL\n\n# for(;;) exit();\nexit:\n  movl $SYS_exit, %eax\n  int $T_SYSCALL\n  jmp exit\n\n# char init&#x5B;] = \"\/init&#92;&#48;\";\ninit:\n  .string \"\/init&#92;&#48;\"\n\n# char *argv&#x5B;] = { init, 0 };\n.p2align 2\nargv:\n  .long init\n  .long 0<\/pre>\n<p>userinit\u95a2\u6570\u306f\u3001\uff08\u6700\u521d\u306f\uff09\u30ab\u30fc\u30cd\u30eb\u7528\u306e\u30e1\u30e2\u30ea\u3060\u3051\u3092\u5bfe\u5fdc\u4ed8\u3051\u308b\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u305f\u3081\u306e\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306bsetupkvm\u95a2\u6570\u3092\u547c\u3076\u3002<br \/>\n\u3053\u306e\u95a2\u6570\u306f\u30ab\u30fc\u30cd\u30eb\u304c\u305d\u308c\u81ea\u8eab\u306e\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u305f\u3081\u306b\u4f7f\u3046\u306e\u3068\u540c\u3058\u3067\u3042\u308b\u3002\uff08\u4ee5\u524d\u3001\u305d\u306e11, 12, 14\u3067\u51fa\u3066\u304d\u307e\u3057\u305f\u306d\u3002\uff09<\/p>\n<p>vm.c\u306esetupkvm\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Set up kernel part of a page table.\npde_t*\nsetupkvm(char* (*alloc)(void))\n{\n  pde_t *pgdir;\n  struct kmap *k;\n\n  if((pgdir = (pde_t*)alloc()) == 0)\n    return 0;\n  memset(pgdir, 0, PGSIZE);\n  if (p2v(PHYSTOP) &gt; (void*)DEVSPACE)\n    panic(&quot;PHYSTOP too high&quot;);\n  for(k = kmap; k &lt; &amp;kmap&amp;#91;NELEM(kmap)&amp;#93;; k++)\n    if(mappages(pgdir, k-&gt;virt, k-&gt;phys_end - k-&gt;phys_start, \n                (uint)k-&gt;phys_start, k-&gt;perm, alloc) &lt; 0)\n      return 0;\n  return pgdir;\n}&amp;#91;\/sourcecode&amp;#93;\n\n\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u30e1\u30e2\u30ea\u306e\u521d\u671f\u306e\u5185\u5bb9\u306f\u3001initcode.S\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u305f\u3082\u306e\u3067\u3042\u308b\u3002\n\u30ab\u30fc\u30cd\u30eb\u304c\u30d7\u30ed\u30bb\u30b9\u3092\u4f5c\u308b\u904e\u7a0b\u306b\u304a\u3044\u3066\u3001\u30ea\u30f3\u30ab\u306f\u305d\u306e\u30d0\u30a4\u30ca\u30ea\u3092\u30ab\u30fc\u30cd\u30eb\u306b\u57cb\u3081\u8fbc\u307f\u3001\u305d\u306e\u4f4d\u7f6e\u3068\u30b5\u30a4\u30ba\u3092\u4f1d\u3048\u308b\u305f\u3081\u306b\u3001\u4e8c\u3064\u306e\u7279\u5225\u306a\u30b7\u30f3\u30dc\u30eb_binary_initcode_start\u3068_binary_initcode_size\u3092\u5b9a\u7fa9\u3059\u308b\u3002\nuserinit\u95a2\u6570\u306f\u3001inituvm\u95a2\u6570\u3092\u4f7f\u3063\u3066\u305d\u306e\u30d0\u30a4\u30ca\u30ea\u3092\u65b0\u3057\u3044\u30d7\u30ed\u30bb\u30b9\u306e\u30e1\u30e2\u30ea\u306b\u30b3\u30d4\u30fc\u3059\u308b\u3002\ninituvm\u95a2\u6570\u306f\u7269\u7406\u30e1\u30e2\u30ea\u306e1\u30da\u30fc\u30b8\u3092\u5272\u308a\u5f53\u3066\u3001\u4eee\u60f3\u30a2\u30c9\u30ec\u30b90\u306b\u30de\u30c3\u30d7\u3057\u3001\u305d\u306e\u30da\u30fc\u30b8\u306b\u305d\u306e\u30d0\u30a4\u30ca\u30ea\u3092\u30b3\u30d4\u30fc\u3059\u308b\u3002\n\nvm.c\u306einituvm\u95a2\u6570\n&amp;#91;sourcecode language=&quot;c&quot;&amp;#93;\/\/ Load the initcode into address 0 of pgdir.\n\/\/ sz must be less than a page.\nvoid\ninituvm(pde_t *pgdir, char *init, uint sz)\n{\n  char *mem;\n  \n  if(sz &gt;= PGSIZE)\n    panic(&quot;inituvm: more than a page&quot;);\n  mem = kalloc();\n  memset(mem, 0, PGSIZE);\n  mappages(pgdir, 0, PGSIZE, v2p(mem), PTE_W|PTE_U, kalloc);\n  memmove(mem, init, sz);\n}<\/pre>\n<p>\u305d\u3046\u3044\u3046\u308f\u3051\u3067\u3001userinit\u95a2\u6570\u306f\u521d\u671f\u306e\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u306e\u72b6\u614b\u3068\u3068\u3082\u306b\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u3002<br \/>\n%cs\u30ec\u30b8\u30b9\u30bf\u306f\u3001\u7279\u6a29\u30ec\u30d9\u30ebDPL_USER\uff08\u4f8b\u3048\u3070\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u306f\u30ab\u30fc\u30cd\u30eb\u30e2\u30fc\u30c9\u3067\u306f\u306a\u3044\uff09\u3067\u5b9f\u884c\u3055\u308c\u3066\u308b\u30bb\u30b0\u30e1\u30f3\u30c8SEG_UCODE\u306e\u305f\u3081\u306e\u30bb\u30b0\u30e1\u30f3\u30c8\u30bb\u30ec\u30af\u30bf\u3092\u542b\u3080\u3002<br \/>\n\u305d\u3057\u3066\u540c\u69d8\u306b\u3001%ds, %es, %ss\u306f\u3001\u7279\u6a29\u30ec\u30d9\u30ebDPL_USER\u3068\u5171\u306bSEG_UDATA\u3092\u4f7f\u3046\u3002<br \/>\n%eflags\u306bFL_IF\u3092\u30bb\u30c3\u30c8\u3057\u3066\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u5272\u308a\u8fbc\u307f\u3092\u8a31\u53ef\u3059\u308b\u3002<br \/>\n\u3053\u306e\u3042\u305f\u308a\u306f\u7b2c2\u7ae0\u3067\u307e\u305f\u8aac\u660e\u3059\u308b\u3002<br \/>\n\uff08\u3053\u306e\u6bb5\u843d\u306f\u3001userinit\u95a2\u6570\u306e\u4e2d\u3067p-&gt;tf\u306e\u4e00\u9023\u306e\u30e1\u30f3\u30d0\u306b\u5024\u3092\u30bb\u30c3\u30c8\u3057\u3066\u308b\u90e8\u5206\u306e\u4e8b\u3092\u8aac\u660e\u3057\u3066\u308b\u307f\u305f\u3044\u3067\u3059\u3002\uff09<\/p>\n<p>\u30b9\u30bf\u30c3\u30af\u30dd\u30a4\u30f3\u30bf%esp\u306f\u3001\u4e00\u756a\u91cd\u8981\u3067\u6709\u52b9\u306a\u4eee\u60f3\u30a2\u30c9\u30ec\u30b9p-&gt;sz\u3067\u3042\u308b\u3002<br \/>\n\u305d\u306e\u547d\u4ee4\u30dd\u30a4\u30f3\u30bf\u306f\u3001initcode\u306e\u958b\u59cb\u70b9\u3067\u3042\u308b\u30a2\u30c9\u30ec\u30b90\u3067\u3042\u308b\u3002<\/p>\n<p>userinit\u95a2\u6570\u306f\u3001\u4e3b\u306b\u30c7\u30d0\u30c3\u30b0\u306e\u305f\u3081\u306binitcode\u306bp-&gt;name\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002<br \/>\np-&gt;cwd\u304c\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u73fe\u5728\u306e\u4f5c\u696d\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u5bfe\u5fdc\u3059\u308b\u3002<br \/>\nnamei\uff08userinit\u95a2\u6570\u5185\u306ep-&gt;cwd = namei(&#8220;\/&#8221;);\u306e\u90e8\u5206\uff09\u306b\u3064\u3044\u3066\u306f\u7b2c5\u7ae0\u3067\u8aac\u660e\u3059\u308b\u3002<\/p>\n<p>\u4e00\u5ea6\u30d7\u30ed\u30bb\u30b9\u304c\u521d\u671f\u5316\u3055\u308c\u305f\u3089\u3001userinit\u306f\u3001p-&gt;state\u3092RUNABLE\u306b\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u3067\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u53ef\u80fd\u3067\u3042\u308b\u5370\u3092\u4ed8\u3051\u308b\u3002<\/p>\n<h3>\u611f\u60f3<\/h3>\n<p>\u9577\u304b\u3063\u305f\u2026<\/p>\n<p>\u30d7\u30ed\u30bb\u30b9\u306e\u751f\u6210\u306b\u3064\u3044\u3066\u306e\u8a71\u3067\u3059\u304c\u3001\u4e3b\u306bproc\u69cb\u9020\u4f53\u3068userinit\u95a2\u6570\u3092\u4e2d\u5fc3\u306b\u8aac\u660e\u3055\u308c\u3066\u307e\u3059\u3002<\/p>\n<p>\u9014\u4e2d\u3084\u305f\u3089\u6307\u793a\u4ee3\u540d\u8a5e\u304c\u591a\u304f\u3066\u8a33\u3069\u3053\u308d\u304b\u30a4\u30e1\u30fc\u30b8\u3092\u601d\u3044\u6d6e\u304b\u3079\u308b\u3053\u3068\u3055\u3048\u96e3\u5100\u3057\u305f\u90e8\u5206\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u305d\u3082\u305d\u3082\u8a00\u8449\u3067\u8aac\u660e\u3059\u308b\u306e\u304c\u96e3\u3057\u3044\u3093\u3067\u3057\u3087\u3046\u306d\u3002<br \/>\n\u901a\u5e38\u306f\u3001\u8aac\u660e\u3092\u982d\u306b\u5165\u308c\u305f\u4e0a\u3067\u30bd\u30fc\u30b9\u3092\u8aad\u3080\u3068\u7406\u89e3\u304c\u65e9\u304b\u3063\u305f\u308a\u3057\u307e\u3059\u304c\u3001\u305d\u3046\u3044\u3046\u3068\u3053\u308d\u306f\u4e00\u65e6\u8b0e\u306e\u8aac\u660e\u306f\u5fd8\u308c\u3066\u9811\u5f35\u3063\u3066\u30bd\u30fc\u30b9\u3092\u3058\u3063\u304f\u308a\u8aad\u307f\u89e3\u304f\u307b\u3046\u304c\u7406\u89e3\u3078\u306e\u8fd1\u9053\u3067\u3057\u3087\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e23\u301c25\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>\u3053\u306e\u7bc0\u3067\u306f\u3001xv6\u304c\u3069\u3046\u3084\u3063\u3066\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u751f\u6210\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u8ff0\u3079\u308b\u3002<br \/>\nxv6\u306e\u30ab\u30fc\u30cd\u30eb\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u3054\u3068\u306b\u591a\u304f\u306e\u72b6\u614b\u3092\u7ba1\u7406\u3059\u308b\u3002<br \/>\n\u305d\u308c\u3089\u306e\u72b6\u614b\u306fproc\u69cb\u9020\u4f53\u306b\u307e\u3068\u3081\u3089\u308c\u3066\u3044\u308b\u3002<br \/>\n\u3072\u3068\u3064\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u72b6\u614b\u3067\u4e00\u756a\u91cd\u8981\u306a\u90e8\u54c1\u306f\u3001\u305d\u308c\u81ea\u8eab\u306e\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u3068\u305d\u308c\u304c\u6307\u3059\u7269\u7406\u30e1\u30e2\u30ea\u3001\u305d\u308c\u81ea\u8eab\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u3001\u305d\u308c\u81ea\u8eab\u306e\u5b9f\u884c\u72b6\u614b\uff08run state\uff09\u3067\u3042\u308b\u3002<br \/>\nproc\u69cb\u9020\u4f53\u306e\u8981\u7d20\u3092\u6307\u3059\u305f\u3081\u306e\u8a18\u6cd5\u3068\u3057\u3066\u3001p-&gt;xxx\u3068\u3044\u3046\u5f62\u3092\u7528\u3044\u308b\u3002<\/p>\n<p>proc.h\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u308bproc\u69cb\u9020\u4f53<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Per-process state\nstruct proc {\n  uint sz;                     \/\/ Size of process memory (bytes)\n  pde_t* pgdir;                \/\/ Page table\n  char *kstack;                \/\/ Bottom of kernel stack for this process\n  enum procstate state;        \/\/ Process state\n  volatile int pid;            \/\/ Process ID\n  struct proc *parent;         \/\/ Parent process\n  struct trapframe *tf;        \/\/ Trap frame for current syscall\n  struct context *context;     \/\/ swtch() here to run process\n  void *chan;                  \/\/ If non-zero, sleeping on chan\n  int killed;                  \/\/ If non-zero, have been killed\n  struct file *ofile&#x5B;NOFILE];  \/\/ Open files\n  struct inode *cwd;           \/\/ Current directory\n  char name&#x5B;16];               \/\/ Process name (debugging)\n};<\/pre>\n<p>\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u72b6\u614b\u3092\u3001\u30d7\u30ed\u30bb\u30b9\u306e\u5473\u65b9\u4e0a\u306e\u30ab\u30fc\u30cd\u30eb\u306e\u4e2d\u3067\u5b9f\u884c\u3055\u308c\u308b\u3001\u5b9f\u884c\u30b9\u30ec\u30c3\u30c9\uff08or thread for short\uff09\u3068\u3057\u3066\u898b\u308b\u3079\u304d\u3067\u3042\u308b\u3002<br \/>\n\uff08\u591a\u5206\u3001\u300c\u30d7\u30ed\u30bb\u30b9\u306e\u5473\u65b9\u4e0a\u306e\u30ab\u30fc\u30cd\u30eb\u300d=\u300c\u30d7\u30ed\u30bb\u30b9\u306e\u4eee\u60f3\u30a2\u30c9\u30ec\u30b9\u7a7a\u9593\u4e0a\u306b\u914d\u7f6e\u3055\u308c\u305f\u30ab\u30fc\u30cd\u30eb\u300d\u3002\u300cor thread for short\u300d\u306e\u65b9\u306f\u3046\u307e\u3044\u8a33\u304c\u601d\u3044\u3064\u304b\u306a\u3044\u3069\u3053\u308d\u304b\u30a4\u30e1\u30fc\u30b8\u3082\u8b0e\uff09<br \/>\n\u30b9\u30ec\u30c3\u30c9\u306f\u3001\u8a08\u7b97\u3092\u5b9f\u884c\u3059\u308b\u3002<br \/>\n\u3057\u304b\u3057\u305d\u308c\u306f\u6b62\u3081\u308c\u308b\u3057\u518d\u958b\u3082\u51fa\u6765\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u30d7\u30ed\u30bb\u30b9\u304c\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u3092\u547c\u3076\u3068\u304d\u3001CPU\u306f\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u5b9f\u884c\u304b\u3089\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u306e\u5b9f\u884c\u3078\u5207\u308a\u66ff\u3048\u308b\u3002<br \/>\n\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u306e\u5b9f\u88c5\uff08\u4f8b\u3048\u3070\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u3093\u3060\u308a\uff09\u3092\u5b9f\u884c\u3059\u308b\u3002<br \/>\n\u305d\u3057\u3066\u305d\u308c\u304b\u3089\u30d7\u30ed\u30bb\u30b9\u306b\u623b\u308b\u3002<br \/>\nxv6\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u304c\u30ab\u30fc\u30cd\u30eb\u306e\u4e2d\u3067I\/O\u306e\u305f\u3081\u306b\u5f85\u3066\u308b\uff08\u3082\u3057\u304f\u306f&#8221;block&#8221;\u3067\u304d\u308b\uff09\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u3001\u305d\u3057\u3066I\/O\u304c\u7d42\u308f\u3063\u305f\u3068\u304d\u306b\u5143\u306e\u5b9f\u884c\u4e2d\u306e\u5834\u6240\u306b\u5fa9\u5e30\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u30b9\u30ec\u30c3\u30c9\u3068\u3057\u3066\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u3092\u5b9f\u884c\u3059\u308b\u3002<br \/>\n\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u306e\u591a\u304f\u306e\u72b6\u614b\uff08\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u3001\u95a2\u6570\u306e\u623b\u308a\u5148\u30a2\u30c9\u30ec\u30b9\uff09\u306f\u3001\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u306e\u30b9\u30bf\u30c3\u30afp-&gt;kstack\u306b\u914d\u7f6e\u3055\u308c\u308b\u3002<br \/>\n\u305d\u308c\u305e\u308c\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306f\u3001\u30e6\u30fc\u30b6\u30b9\u30bf\u30c3\u30af\u3092\u58ca\u3055\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306b\u3001\u30e6\u30fc\u30b6\u30b9\u30bf\u30c3\u30af\u3068\u306f\u5225\u306b\u306a\u3063\u3066\u3044\u308b\u3002<br \/>\n\u3064\u307e\u308a\u30d7\u30ed\u30bb\u30b9\u3092\u30012\u3064\u306e\u5b9f\u884c\u30b9\u30ec\u30c3\u30c9\u3092\u6301\u3063\u3066\u3044\u308b\u3068\u307f\u306a\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br \/>\n\u4e00\u3064\u306f\u30e6\u30fc\u30b6\u30b9\u30ec\u30c3\u30c9\u3001\u3082\u3046\u4e00\u3064\u306f\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u3067\u3042\u308b\u3002<\/p>\n<p>p-&gt;state\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u304c\u5272\u308a\u5f53\u3066\u6e08\u307f\u304b\u3001\u5b9f\u884c\u6e96\u5099\u304c\u5b8c\u4e86\u3057\u3066\u308b\u304b\u3001\u5b9f\u884c\u4e2d\u304b\u3001I\/O\u5f85\u3061\u304b\u3001\u7d42\u4e86\u4e2d\u304b\u3069\u3046\u304b\u3092\u793a\u3059\u3002<\/p>\n<p>p-&gt;pgdir\u306f\u3001PTE\u306e\u914d\u5217\u3092\u4fdd\u6301\u3059\u308b\u3002<br \/>\nxv6\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u304c\u5b9f\u884c\u3055\u308c\u308b\u3068\u304d\u3001\u30da\u30fc\u30b8\u30f3\u30b0\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u306b\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306ep-&gt;pgdir\u3092\u4f7f\u308f\u305b\u308b\u3088\u3046\u306b\u3059\u308b\u3002<br \/>\n\u30d7\u30ed\u30bb\u30b9\u306e\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u306f\u307e\u305f\u3001\u30d7\u30ed\u30bb\u30b9\u306e\u30e1\u30e2\u30ea\u3092\u4fdd\u6301\u3059\u308b\u305f\u3081\u306b\u5272\u308a\u5f53\u3066\u6e08\u307f\u306e\u7269\u7406\u30da\u30fc\u30b8\u306e\u30a2\u30c9\u30ec\u30b9\u306e\u30ec\u30b3\u30fc\u30c9\u3068\u3057\u3066\u52d5\u304f\u3002<\/p>\n<p>\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u751f\u6210\u3059\u308b\u6d41\u308c\u306f\u3001main\u95a2\u6570\u304cuserinit\u95a2\u6570\u3092\u547c\u3076\u3068\u3053\u308d\u304b\u3089\u59cb\u307e\u308b\u3002<br \/>\nuserinit\u95a2\u6570\u306e\u6700\u521d\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u306fallocproc\u95a2\u6570\u3092\u547c\u3076\u3053\u3068\u3067\u3042\u308b\u3002<br \/>\nallocproc\u306e\u4ed5\u4e8b\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u30c6\u30fc\u30d6\u30eb\u306b\u30b9\u30ed\u30c3\u30c8\uff08proc\u69cb\u9020\u4f53\uff09\u3092\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u3068\u3001\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u30d7\u30ed\u30bb\u30b9\u72b6\u614b\u306e\u4e00\u90e8\u306e\u521d\u671f\u5316\u3067\u3042\u308b\u3002<br \/>\nuserinit\u304c\u4e00\u756a\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u305f\u3081\u306b\u547c\u3070\u308c\u308b\u9593\u3001allocproc\u306f\u3001\u5168\u3066\u306e\u65b0\u3057\u3044\u30d7\u30ed\u30bb\u30b9\u306e\u305f\u3081\u306b\u547c\u3070\u308c\u308b\u3002<br \/>\nallocproc\u306f\u3001UNUSED\u72b6\u614b\u3092\u30ad\u30fc\u306b\u30d7\u30ed\u30bb\u30b9\u30c6\u30fc\u30d6\u30eb\u3092\u30b9\u30ad\u30e3\u30f3\u3059\u308b\u3002<br \/>\n\u672a\u4f7f\u7528\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u898b\u3064\u3051\u305f\u3068\u304d\u3001allocproc\u306f\u4f7f\u7528\u4e2d\u3092\u793a\u3059\u305f\u3081EMBRYO\u3092\u30bb\u30c3\u30c8\u3057\u3001\u4e00\u610f\u306epid\u3092\u4ed8\u4e0e\u3059\u308b\u3002<br \/>\n\u6b21\u306b\u3001\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u306e\u305f\u3081\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u3092\u5272\u308a\u5f53\u3066\u3088\u3046\u3068\u3059\u308b\u3002<br \/>\n\u3082\u3057\u5272\u308a\u5f53\u3066\u304c\u5931\u6557\u3057\u305f\u3089\u3001allocproc\u306f\u72b6\u614b\u3092UNUSED\u306b\u623b\u3057\u3001\u5931\u6557\u3092\u4f1d\u3048\u308b\u305f\u3081\u306b\u30bc\u30ed\u3092\u8fd4\u3059\u3002<\/p>\n<p>main.c\u306emain\u95a2\u6570\uff08\u53c2\u7167\u30b3\u30df\u30c3\u30c8\u304c\u5909\u308f\u3063\u305f\u306e\u3067\u4ee5\u524d\u8f09\u305b\u305f\u3082\u306e\u3068\u306f\u9055\u3046\u306f\u305a\u3067\u3059\u3002\uff09<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ 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>proc.c\u306euserinit\u95a2\u6570\u3068allocproc\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/PAGEBREAK: 32\n\/\/ Set up first user process.\nvoid\nuserinit(void)\n{\n  struct proc *p;\n  extern char _binary_initcode_start&#x5B;], _binary_initcode_size&#x5B;];\n  \n  p = allocproc();\n  initproc = p;\n  if((p-&gt;pgdir = setupkvm(kalloc)) == 0)\n    panic(&quot;userinit: out of memory?&quot;);\n  inituvm(p-&gt;pgdir, _binary_initcode_start, (int)_binary_initcode_size);\n  p-&gt;sz = PGSIZE;\n  memset(p-&gt;tf, 0, sizeof(*p-&gt;tf));\n  p-&gt;tf-&gt;cs = (SEG_UCODE &lt;&lt; 3) | DPL_USER;\n  p-&gt;tf-&gt;ds = (SEG_UDATA &lt;&lt; 3) | DPL_USER;\n  p-&gt;tf-&gt;es = p-&gt;tf-&gt;ds;\n  p-&gt;tf-&gt;ss = p-&gt;tf-&gt;ds;\n  p-&gt;tf-&gt;eflags = FL_IF;\n  p-&gt;tf-&gt;esp = PGSIZE;\n  p-&gt;tf-&gt;eip = 0;  \/\/ beginning of initcode.S\n\n  safestrcpy(p-&gt;name, &quot;initcode&quot;, sizeof(p-&gt;name));\n  p-&gt;cwd = namei(&quot;\/&quot;);\n\n  p-&gt;state = RUNNABLE;\n}\n\n\/\/PAGEBREAK: 32\n\/\/ Look in the process table for an UNUSED proc.\n\/\/ If found, change state to EMBRYO and initialize\n\/\/ state required to run in the kernel.\n\/\/ Otherwise return 0.\nstatic struct proc*\nallocproc(void)\n{\n  struct proc *p;\n  char *sp;\n\n  acquire(&amp;ptable.lock);\n  for(p = ptable.proc; p &lt; &amp;ptable.proc&amp;#91;NPROC&amp;#93;; p++)\n    if(p-&gt;state == UNUSED)\n      goto found;\n  release(&amp;ptable.lock);\n  return 0;\n\nfound:\n  p-&gt;state = EMBRYO;\n  p-&gt;pid = nextpid++;\n  release(&amp;ptable.lock);\n\n  \/\/ Allocate kernel stack.\n  if((p-&gt;kstack = kalloc()) == 0){\n    p-&gt;state = UNUSED;\n    return 0;\n  }\n  sp = p-&gt;kstack + KSTACKSIZE;\n  \n  \/\/ Leave room for trap frame.\n  sp -= sizeof *p-&gt;tf;\n  p-&gt;tf = (struct trapframe*)sp;\n  \n  \/\/ Set up new context to start executing at forkret,\n  \/\/ which returns to trapret.\n  sp -= 4;\n  *(uint*)sp = (uint)trapret;\n\n  sp -= sizeof *p-&gt;context;\n  p-&gt;context = (struct context*)sp;\n  memset(p-&gt;context, 0, sizeof *p-&gt;context);\n  p-&gt;context-&gt;eip = (uint)forkret;\n\n  return p;\n}<\/pre>\n<p>\u3055\u3066\u3053\u3053\u3067\u3001allocproc\u306f\u65b0\u3057\u3044\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\n\u901a\u5e38\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u306ffork\u306b\u3088\u3063\u3066\u306e\u307f\u4f5c\u3089\u308c\u308b\u3002<br \/>\n\u306a\u306e\u3067\u3001\u65b0\u3057\u3044\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u89aa\u304b\u3089\u30b3\u30d4\u30fc\u3055\u308c\u3066\u59cb\u307e\u308b\u3002<br \/>\nfork\u306e\u7d50\u679c\u3001\u5185\u5bb9\u306f\u89aa\u3068\u540c\u3058\u3060\u304c\u72ec\u7acb\u306a\u30e1\u30e2\u30ea\u3092\u6301\u3064\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u751f\u307e\u308c\u308b\u3002<br \/>\nallocproc\u306f\u3001\u7279\u5225\u306b\u7528\u610f\u3055\u308c\u305f\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u3068\u3001\u89aa\u3068\u540c\u3058\u30e6\u30fc\u30b6\u7a7a\u9593\u4e0a\u306e\u5834\u6240\uff08fork\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u304b\u3089\u623b\u308b\u5834\u6240\uff09\u306b\u623b\u308b\u305f\u3081\u306e\u30ab\u30fc\u30cd\u30eb\u30ec\u30b8\u30b9\u30bf\u306e\u7d44\u3067\u3001\u305d\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u3067\u306e\u5b9f\u884c\u3092\u958b\u59cb\u3059\u308b\u305f\u3081\u5b50\u30d7\u30ed\u30bb\u30b9\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u3002<br \/>\n\u305d\u306e\u7528\u610f\u3055\u308c\u305f\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306e\u30ec\u30a4\u30a2\u30a6\u30c8\u306f\u56f31-3\u306b\u793a\u3059\u3002<br \/>\nallocproc\u306f\u3053\u306e\u4ed5\u4e8b\u306e\u4e00\u90e8\u3092\u3001\u30ea\u30bf\u30fc\u30f3\u30d7\u30ed\u30b0\u30e9\u30e0\u30ab\u30a6\u30f3\u30bf\u5024\uff08return program counter values\uff09\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u3053\u3068\u3067\u3053\u306a\u3059\u3002<br \/>\n\u30ea\u30bf\u30fc\u30f3\u30d7\u30ed\u30b0\u30e9\u30e0\u30ab\u30a6\u30f3\u30bf\u5024\u306f\u3001\u65b0\u3057\u3044\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u3092\u307e\u305aforkret\u3067\u5b9f\u884c\u3057\u305d\u3057\u3066trapret\u3067\u5b9f\u884c\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u3002\uff08allocproc\u95a2\u6570\u306e\u6700\u5f8c\u306e\u3042\u305f\u308a\u3092\u53c2\u7167\u3002\u30b3\u30fc\u30c9\u4e0a\u3067\u306f\u307e\u305a\u623b\u308a\u5148\u306etrapret\u3092\u8a2d\u5b9a\u3057\u3066\u304b\u3089\u98db\u3073\u5148\u306eforkret\u3092\u8a2d\u5b9a\u3057\u3066\u308b\u3002\uff09<br \/>\n\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u306f\u3001p-&gt;context\u304b\u3089\u30b3\u30d4\u30fc\u3055\u308c\u305f\u30ec\u30b8\u30b9\u30bf\u306e\u5185\u5bb9\u3067\u5b9f\u884c\u3092\u958b\u59cb\u3059\u308b\u3002<br \/>\n\u8981\u3059\u308b\u306b\u3001p-&gt;context-&gt;eip\u3092forkret\u306b\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u306f\u3001\u30ab\u30fc\u30cd\u30eb\u30b9\u30ec\u30c3\u30c9\u3092forkret\u306e\u306f\u3058\u3081\u304b\u3089\u5b9f\u884c\u3059\u308b\u3053\u3068\u3092\u5f15\u304d\u8d77\u3053\u3059\u3002\uff08\u5f8c\u306b\u8f09\u305b\u307e\u3059\u304c\u3001forkret\u306f\u95a2\u6570\u3067\u3059\u3002\u3064\u307e\u308a\u95a2\u6570\u30dd\u30a4\u30f3\u30bf\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\uff09<br \/>\n\u3053\u306e\u95a2\u6570\u306f\u3001\u30b9\u30bf\u30c3\u30af\u306e\u4e0b\u306b\u3042\u308b\u30a2\u30c9\u30ec\u30b9\u306b\u3068\u306b\u304b\u304f\u623b\u308b\u3002<br \/>\n\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30b9\u30a4\u30c3\u30c1\u306e\u30b3\u30fc\u30c9\uff08\u4ee5\u4e0b\u306eswitch.S\u53c2\u7167\uff09\u306f\u3001p-&gt;context\u306e\u7d42\u308f\u308a\u306e\u3059\u3050\u6b21\u3092\u6307\u3059\u3088\u3046\u306b\u30b9\u30bf\u30c3\u30af\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002<br \/>\nallocproc\u306f\u3001\u30b9\u30bf\u30c3\u30af\u4e0a\u306bp-&gt;context\u3092\u7f6e\u304d\u3001\u305d\u3057\u3066trapret\u3078\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u3059\u3050\u305d\u306e\u4e0a\u306b\u7f6e\u304f\u3002<br \/>\n\u305d\u308c\u304cforkret\u306e\u623b\u308a\u5148\u3068\u306a\u308b\u3002<br \/>\ntrapret\u306f\u3001\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u306b\u4fdd\u6301\u3055\u308c\u305f\u5024\u304b\u3089\u30e6\u30fc\u30b6\u30ec\u30b8\u30b9\u30bf\u3092\u5fa9\u5143\u3057\u3001\u30d7\u30ed\u30bb\u30b9\u306b\u30b8\u30e3\u30f3\u30d7\u3059\u308b\u3002\uff08\u4ee5\u4e0b\u306etrapasm.S\u53c2\u7167\uff09<br \/>\n\u3053\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u306f\u3001\u901a\u5e38\u306efork\u3084\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u751f\u6210\u3068\u540c\u3058\u3067\u3042\u308b\u3002<br \/>\n\u305f\u3060\u3001\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u751f\u6210\u306e\u5834\u5408\u306f\u3001fork\u304b\u3089\u8fd4\u3063\u3066\u304d\u305f\u3068\u3053\u308d\u304b\u3089\u3068\u3044\u3046\u3088\u308a\u3080\u3057\u308d\u3001\u4f4d\u7f6e\u30bc\u30ed\u304b\u3089\u5b9f\u884c\u3092\u59cb\u3081\u308b\u3002<\/p>\n<p>\u56f31-3 \u65b0\u3057\u3044\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7<br \/>\n<a href=\"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\"><img loading=\"lazy\" decoding=\"async\" src=\"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\" alt=\"\" title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2012-02-19 9.26.23\" width=\"474\" height=\"483\" class=\"aligncenter size-full wp-image-1288\" srcset=\"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 474w, 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 294w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<p>proc.c\u306eforkret\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ A fork child's very first scheduling by scheduler()\n\/\/ will swtch here.  \"Return\" to user space.\nvoid\nforkret(void)\n{\n  static int first = 1;\n  \/\/ Still holding ptable.lock from scheduler.\n  release(&ptable.lock);\n\n  if (first) {\n    \/\/ Some initialization functions must be run in the context\n    \/\/ of a regular process (e.g., they call sleep), and thus cannot \n    \/\/ be run from main().\n    first = 0;\n    initlog();\n  }\n  \n  \/\/ Return to \"caller\", actually trapret (see allocproc).\n}<\/pre>\n<p>switch.S\u306eswitch\uff08\u3053\u308c\u3067switch.S\u306e\u5168\u3066\u3067\u3059\u3002\uff09<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\"># Context switch\n#\n#   void swtch(struct context **old, struct context *new);\n# \n# Save current register context in old\n# and then load register context from new.\n\n.globl swtch\nswtch:\n  movl 4(%esp), %eax\n  movl 8(%esp), %edx\n\n  # Save old callee-save registers\n  pushl %ebp\n  pushl %ebx\n  pushl %esi\n  pushl %edi\n\n  # Switch stacks\n  movl %esp, (%eax)\n  movl %edx, %esp\n\n  # Load new callee-save registers\n  popl %edi\n  popl %esi\n  popl %ebx\n  popl %ebp\n  ret\n\ntrapasm.S\u306etrapret\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<\/pre>\n<p>\u7b2c2\u7ae0\u306b\u8f09\u3063\u3066\u308b\u304c\u3001\u30e6\u30fc\u30b6\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u304b\u3089\u30ab\u30fc\u30cd\u30eb\u3078\u306e\u5236\u5fa1\u3092\u79fb\u52d5\u3059\u308b\u65b9\u6cd5\u306f\u3001\u5272\u308a\u8fbc\u307f\u306e\u4ed5\u7d44\u307f\u3092\u5143\u306b\u3057\u3066\u3044\u308b\u3002<br \/>\n\u305d\u306e\u4ed5\u7d44\u307f\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u3084\u4e2d\u65ad\u3084\u4f8b\u5916\u3067\u4f7f\u308f\u308c\u308b\u3002<br \/>\n\u30d7\u30ed\u30bb\u30b9\u304c\u5b9f\u884c\u3055\u308c\u3066\u308b\u9593\u3001\u5236\u5fa1\u3092\u30ab\u30fc\u30cd\u30eb\u306b\u79fb\u3059\u3068\u304d\u306f\u3044\u3064\u3067\u3082\u3001\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u3068xv6\u306f\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u306b\u30e6\u30fc\u30b6\u30ec\u30b8\u30b9\u30bf\u3092\u4fdd\u5b58\u3057\u3001\u305d\u306e\u3068\u304d\u3068\u306e\u30b3\u30fc\u30c9\u3092\u30c8\u30e9\u30c3\u30d7\u3059\u308b\u3002<br \/>\nuserinit\u95a2\u6570\u306f\u3001\u3082\u3057\u30d7\u30ed\u30bb\u30b9\u304c\u5272\u308a\u8fbc\u307f\u7d4c\u7531\uff08userinit\u95a2\u6570\u306e\u4e2d\u3067p-&gt;tf\u306e\u4e00\u9023\u306e\u30e1\u30f3\u30d0\u306b\u5024\u3092\u30bb\u30c3\u30c8\u3057\u3066\u308b\u90e8\u5206\uff09\u3067\u30ab\u30fc\u30cd\u30eb\u306b\u5165\u3063\u305f\u3089\u3001\u65b0\u3057\u3044\u30b9\u30bf\u30c3\u30af\u306e\u30c8\u30c3\u30d7\u306b\u3001\u305d\u3053\u306b\u3042\u308b\u3067\u3042\u308d\u3046\u5f7c\u3089\u306b\u305d\u3063\u304f\u308a\u306a\u5024\u3092\u66f8\u304d\u8fbc\u3080\u3002<br \/>\n\u3060\u304b\u3089\u3001\u30ab\u30fc\u30cd\u30eb\u304b\u3089\u30d7\u30ed\u30bb\u30b9\u306e\u30e6\u30fc\u30b6\u30b3\u30fc\u30c9\u306b\u623b\u308b\u901a\u5e38\u306e\u30b3\u30fc\u30c9\u306f\u3046\u307e\u304f\u52d5\u4f5c\u3059\u308b\u3002<br \/>\n\u305d\u308c\u3089\u306e\u5024\u306f\u3001\u30e6\u30fc\u30b6\u30ec\u30b8\u30b9\u30bf\u306b\u4fdd\u6301\u3055\u308c\u308btrapframe\u69cb\u9020\u4f53\u3067\u3042\u308b\u3002<br \/>\n\u305d\u3046\u3084\u3063\u3066\u30d7\u30ed\u30bb\u30b9\u306e\u30ab\u30fc\u30cd\u30eb\u30b9\u30bf\u30c3\u30af\u306f\u3001\u56f31-3\u3067\u793a\u3055\u308c\u308b\u3088\u3046\u306a\u6e96\u5099\u5b8c\u4e86\u72b6\u614b\u306b\u306a\u308b\u3002<\/p>\n<p>\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u5c0f\u3055\u3044\u30d7\u30ed\u30b0\u30e9\u30e0\uff08initcode.S\uff09\u3092\u5b9f\u884c\u3059\u308b\u3002<br \/>\n\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u3053\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4fdd\u6301\u3059\u308b\u305f\u3081\u306e\u7269\u7406\u30e1\u30e2\u30ea\u304c\u5fc5\u8981\u3067\u3042\u308b\u3002<br \/>\n\u305d\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001\u305d\u306e\u30e1\u30e2\u30ea\u306b\u30b3\u30d4\u30fc\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u305d\u3057\u3066\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u305d\u306e\u30e1\u30e2\u30ea\u3092\u6307\u3059\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u3092\u5fc5\u8981\u3068\u3059\u308b\u3002<\/p>\n<p>initcode.S\uff08\u3053\u308c\u3067\u5168\u3066\u3067\u3059\u3002\uff09<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\"># Initial process execs \/init.\n\n#include \"syscall.h\"\n#include \"traps.h\"\n\n\n# exec(init, argv)\n.globl start\nstart:\n  pushl $argv\n  pushl $init\n  pushl $0  \/\/ where caller pc would be\n  movl $SYS_exec, %eax\n  int $T_SYSCALL\n\n# for(;;) exit();\nexit:\n  movl $SYS_exit, %eax\n  int $T_SYSCALL\n  jmp exit\n\n# char init&#x5B;] = \"\/init&#92;&#48;\";\ninit:\n  .string \"\/init&#92;&#48;\"\n\n# char *argv&#x5B;] = { init, 0 };\n.p2align 2\nargv:\n  .long init\n  .long 0<\/pre>\n<p>userinit\u95a2\u6570\u306f\u3001\uff08\u6700\u521d\u306f\uff09\u30ab\u30fc\u30cd\u30eb\u7528\u306e\u30e1\u30e2\u30ea\u3060\u3051\u3092\u5bfe\u5fdc\u4ed8\u3051\u308b\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u305f\u3081\u306e\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306bsetupkvm\u95a2\u6570\u3092\u547c\u3076\u3002<br \/>\n\u3053\u306e\u95a2\u6570\u306f\u30ab\u30fc\u30cd\u30eb\u304c\u305d\u308c\u81ea\u8eab\u306e\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u305f\u3081\u306b\u4f7f\u3046\u306e\u3068\u540c\u3058\u3067\u3042\u308b\u3002\uff08\u4ee5\u524d\u3001\u305d\u306e11, 12, 14\u3067\u51fa\u3066\u304d\u307e\u3057\u305f\u306d\u3002\uff09<\/p>\n<p>vm.c\u306esetupkvm\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Set up kernel part of a page table.\npde_t*\nsetupkvm(char* (*alloc)(void))\n{\n  pde_t *pgdir;\n  struct kmap *k;\n\n  if((pgdir = (pde_t*)alloc()) == 0)\n    return 0;\n  memset(pgdir, 0, PGSIZE);\n  if (p2v(PHYSTOP) &gt; (void*)DEVSPACE)\n    panic(&quot;PHYSTOP too high&quot;);\n  for(k = kmap; k &lt; &amp;kmap&amp;#91;NELEM(kmap)&amp;#93;; k++)\n    if(mappages(pgdir, k-&gt;virt, k-&gt;phys_end - k-&gt;phys_start, \n                (uint)k-&gt;phys_start, k-&gt;perm, alloc) &lt; 0)\n      return 0;\n  return pgdir;\n}&amp;#91;\/sourcecode&amp;#93;\n\n\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u30e1\u30e2\u30ea\u306e\u521d\u671f\u306e\u5185\u5bb9\u306f\u3001initcode.S\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u305f\u3082\u306e\u3067\u3042\u308b\u3002\n\u30ab\u30fc\u30cd\u30eb\u304c\u30d7\u30ed\u30bb\u30b9\u3092\u4f5c\u308b\u904e\u7a0b\u306b\u304a\u3044\u3066\u3001\u30ea\u30f3\u30ab\u306f\u305d\u306e\u30d0\u30a4\u30ca\u30ea\u3092\u30ab\u30fc\u30cd\u30eb\u306b\u57cb\u3081\u8fbc\u307f\u3001\u305d\u306e\u4f4d\u7f6e\u3068\u30b5\u30a4\u30ba\u3092\u4f1d\u3048\u308b\u305f\u3081\u306b\u3001\u4e8c\u3064\u306e\u7279\u5225\u306a\u30b7\u30f3\u30dc\u30eb_binary_initcode_start\u3068_binary_initcode_size\u3092\u5b9a\u7fa9\u3059\u308b\u3002\nuserinit\u95a2\u6570\u306f\u3001inituvm\u95a2\u6570\u3092\u4f7f\u3063\u3066\u305d\u306e\u30d0\u30a4\u30ca\u30ea\u3092\u65b0\u3057\u3044\u30d7\u30ed\u30bb\u30b9\u306e\u30e1\u30e2\u30ea\u306b\u30b3\u30d4\u30fc\u3059\u308b\u3002\ninituvm\u95a2\u6570\u306f\u7269\u7406\u30e1\u30e2\u30ea\u306e1\u30da\u30fc\u30b8\u3092\u5272\u308a\u5f53\u3066\u3001\u4eee\u60f3\u30a2\u30c9\u30ec\u30b90\u306b\u30de\u30c3\u30d7\u3057\u3001\u305d\u306e\u30da\u30fc\u30b8\u306b\u305d\u306e\u30d0\u30a4\u30ca\u30ea\u3092\u30b3\u30d4\u30fc\u3059\u308b\u3002\n\nvm.c\u306einituvm\u95a2\u6570\n&amp;#91;sourcecode language=&quot;c&quot;&amp;#93;\/\/ Load the initcode into address 0 of pgdir.\n\/\/ sz must be less than a page.\nvoid\ninituvm(pde_t *pgdir, char *init, uint sz)\n{\n  char *mem;\n  \n  if(sz &gt;= PGSIZE)\n    panic(&quot;inituvm: more than a page&quot;);\n  mem = kalloc();\n  memset(mem, 0, PGSIZE);\n  mappages(pgdir, 0, PGSIZE, v2p(mem), PTE_W|PTE_U, kalloc);\n  memmove(mem, init, sz);\n}<\/pre>\n<p>\u305d\u3046\u3044\u3046\u308f\u3051\u3067\u3001userinit\u95a2\u6570\u306f\u521d\u671f\u306e\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u306e\u72b6\u614b\u3068\u3068\u3082\u306b\u30c8\u30e9\u30c3\u30d7\u30d5\u30ec\u30fc\u30e0\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u3002<br \/>\n%cs\u30ec\u30b8\u30b9\u30bf\u306f\u3001\u7279\u6a29\u30ec\u30d9\u30ebDPL_USER\uff08\u4f8b\u3048\u3070\u30e6\u30fc\u30b6\u30e2\u30fc\u30c9\u306f\u30ab\u30fc\u30cd\u30eb\u30e2\u30fc\u30c9\u3067\u306f\u306a\u3044\uff09\u3067\u5b9f\u884c\u3055\u308c\u3066\u308b\u30bb\u30b0\u30e1\u30f3\u30c8SEG_UCODE\u306e\u305f\u3081\u306e\u30bb\u30b0\u30e1\u30f3\u30c8\u30bb\u30ec\u30af\u30bf\u3092\u542b\u3080\u3002<br \/>\n\u305d\u3057\u3066\u540c\u69d8\u306b\u3001%ds, %es, %ss\u306f\u3001\u7279\u6a29\u30ec\u30d9\u30ebDPL_USER\u3068\u5171\u306bSEG_UDATA\u3092\u4f7f\u3046\u3002<br \/>\n%eflags\u306bFL_IF\u3092\u30bb\u30c3\u30c8\u3057\u3066\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u5272\u308a\u8fbc\u307f\u3092\u8a31\u53ef\u3059\u308b\u3002<br \/>\n\u3053\u306e\u3042\u305f\u308a\u306f\u7b2c2\u7ae0\u3067\u307e\u305f\u8aac\u660e\u3059\u308b\u3002<br \/>\n\uff08\u3053\u306e\u6bb5\u843d\u306f\u3001userinit\u95a2\u6570\u306e\u4e2d\u3067p-&gt;tf\u306e\u4e00\u9023\u306e\u30e1\u30f3\u30d0\u306b\u5024\u3092\u30bb\u30c3\u30c8\u3057\u3066\u308b\u90e8\u5206\u306e\u4e8b\u3092\u8aac\u660e\u3057\u3066\u308b\u307f\u305f\u3044\u3067\u3059\u3002\uff09<\/p>\n<p>\u30b9\u30bf\u30c3\u30af\u30dd\u30a4\u30f3\u30bf%esp\u306f\u3001\u4e00\u756a\u91cd\u8981\u3067\u6709\u52b9\u306a\u4eee\u60f3\u30a2\u30c9\u30ec\u30b9p-&gt;sz\u3067\u3042\u308b\u3002<br \/>\n\u305d\u306e\u547d\u4ee4\u30dd\u30a4\u30f3\u30bf\u306f\u3001initcode\u306e\u958b\u59cb\u70b9\u3067\u3042\u308b\u30a2\u30c9\u30ec\u30b90\u3067\u3042\u308b\u3002<\/p>\n<p>userinit\u95a2\u6570\u306f\u3001\u4e3b\u306b\u30c7\u30d0\u30c3\u30b0\u306e\u305f\u3081\u306binitcode\u306bp-&gt;name\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002<br \/>\np-&gt;cwd\u304c\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u73fe\u5728\u306e\u4f5c\u696d\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u5bfe\u5fdc\u3059\u308b\u3002<br \/>\nnamei\uff08userinit\u95a2\u6570\u5185\u306ep-&gt;cwd = namei(&#8220;\/&#8221;);\u306e\u90e8\u5206\uff09\u306b\u3064\u3044\u3066\u306f\u7b2c5\u7ae0\u3067\u8aac\u660e\u3059\u308b\u3002<\/p>\n<p>\u4e00\u5ea6\u30d7\u30ed\u30bb\u30b9\u304c\u521d\u671f\u5316\u3055\u308c\u305f\u3089\u3001userinit\u306f\u3001p-&gt;state\u3092RUNABLE\u306b\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u3067\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u53ef\u80fd\u3067\u3042\u308b\u5370\u3092\u4ed8\u3051\u308b\u3002<\/p>\n<h3>\u611f\u60f3<\/h3>\n<p>\u9577\u304b\u3063\u305f\u2026<\/p>\n<p>\u30d7\u30ed\u30bb\u30b9\u306e\u751f\u6210\u306b\u3064\u3044\u3066\u306e\u8a71\u3067\u3059\u304c\u3001\u4e3b\u306bproc\u69cb\u9020\u4f53\u3068userinit\u95a2\u6570\u3092\u4e2d\u5fc3\u306b\u8aac\u660e\u3055\u308c\u3066\u307e\u3059\u3002<\/p>\n<p>\u9014\u4e2d\u3084\u305f\u3089\u6307\u793a\u4ee3\u540d\u8a5e\u304c\u591a\u304f\u3066\u8a33\u3069\u3053\u308d\u304b\u30a4\u30e1\u30fc\u30b8\u3092\u601d\u3044\u6d6e\u304b\u3079\u308b\u3053\u3068\u3055\u3048\u96e3\u5100\u3057\u305f\u90e8\u5206\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u305d\u3082\u305d\u3082\u8a00\u8449\u3067\u8aac\u660e\u3059\u308b\u306e\u304c\u96e3\u3057\u3044\u3093\u3067\u3057\u3087\u3046\u306d\u3002<br \/>\n\u901a\u5e38\u306f\u3001\u8aac\u660e\u3092\u982d\u306b\u5165\u308c\u305f\u4e0a\u3067\u30bd\u30fc\u30b9\u3092\u8aad\u3080\u3068\u7406\u89e3\u304c\u65e9\u304b\u3063\u305f\u308a\u3057\u307e\u3059\u304c\u3001\u305d\u3046\u3044\u3046\u3068\u3053\u308d\u306f\u4e00\u65e6\u8b0e\u306e\u8aac\u660e\u306f\u5fd8\u308c\u3066\u9811\u5f35\u3063\u3066\u30bd\u30fc\u30b9\u3092\u3058\u3063\u304f\u308a\u8aad\u307f\u89e3\u304f\u307b\u3046\u304c\u7406\u89e3\u3078\u306e\u8fd1\u9053\u3067\u3057\u3087\u3046\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-1285","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\/1285","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=1285"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1285\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=1285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=1285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=1285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}