{"id":1441,"date":"2012-03-14T09:51:06","date_gmt":"2012-03-14T00:51:06","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=1441"},"modified":"2012-03-14T11:01:09","modified_gmt":"2012-03-14T02:01:09","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%ae40","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/1441","title":{"rendered":"[xv6 #40] Chapter 3 &#8211; Locking &#8211; Memory ordering"},"content":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e48\u301c49\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>\u3053\u306e\u7ae0\u3067\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u304c\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4e2d\u3067\u73fe\u308c\u308b\u9806\u756a\u3067\u4e00\u9023\u306e\u547d\u4ee4\u3092\u958b\u59cb\u3057\u5b8c\u4e86\u3059\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3002<br \/>\n\u3057\u304b\u3057\u306a\u304c\u3089\u591a\u304f\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u3001\u3088\u308a\u9ad8\u3044\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u9054\u6210\u3059\u308b\u305f\u3081\u306b\u547d\u4ee4\u3092\u9806\u756a\u901a\u308a\u306b\u306f\u5b9f\u884c\u3057\u306a\u3044\u3002\uff08\u30a2\u30a6\u30c8\u30aa\u30d6\u30aa\u30fc\u30c0\u30fc\u5b9f\u884c\uff09<br \/>\n\u3042\u308b\u547d\u4ee4\u304c\u3001\u305d\u306e\u5b8c\u4e86\u307e\u3067\u306b\u591a\u304f\u306e\u30b5\u30a4\u30af\u30eb\u3092\u8981\u3059\u308b\u5834\u5408\u3001\u4ed6\u306e\u547d\u4ee4\u3068\u30aa\u30fc\u30d0\u30fc\u30e9\u30c3\u30d7\u3057\u3066\u5b9f\u884c\u3057\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u30b9\u30c8\u30fc\u30eb\u3092\u907f\u308b\u305f\u3081\u306b\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u305d\u306e\u547d\u4ee4\u3092\u65e9\u3081\u306b\u767a\u884c\u3057\u305f\u3044\u306f\u305a\u3067\u3042\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u76f4\u5217\u306b\u4e26\u3093\u3067\u3044\u308b\u547d\u4ee4A\u3068\u547d\u4ee4B\u304c\u304a\u4e92\u3044\u306b\u4f9d\u5b58\u3057\u3066\u306a\u3044\u4e8b\u306b\u6c17\u3065\u304d\u3001\u547d\u4ee4A\u3092\u5b8c\u4e86\u3059\u308b\u3068\u304d\u306b\u547d\u4ee4B\u3082\u5b8c\u4e86\u3055\u305b\u308b\u305f\u3081\u306b\u3001\u547d\u4ee4A\u306e\u524d\u306b\u547d\u4ee4B\u3092\u958b\u59cb\u3059\u308b\u3002<br \/>\n\u3057\u304b\u3057\u306a\u304c\u3089\u540c\u6642\u5b9f\u884c\u306f\u3001\u547d\u4ee4\u306e\u4e26\u3073\u66ff\u3048\u306b\u3088\u3063\u3066\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u3001\u9593\u9055\u3063\u305f\u632f\u308b\u821e\u3044\u3092\u767a\u751f\u3055\u305b\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001release\u95a2\u6570\u304cxchg\u547d\u4ee4\u3092\u4f7f\u308f\u305a\u306blk-&amp;gt;locked\u306b0\u3092\u5272\u308a\u5f53\u3066\u308b\u5834\u5408\u3001\u4f55\u304c\u8d77\u304d\u308b\u306e\u304b\u5fc3\u914d\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3060\u308d\u3046\u3002<br \/>\nx86\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u3001\u4e16\u4ee3\u306b\u3088\u3063\u3066\u4fdd\u8a3c\u3059\u308b\u30e1\u30e2\u30ea\u306e\u9806\u5e8f\u6027\u306b\u9055\u3044\u304c\u3042\u308b\u305f\u3081\u3001\u3053\u306e\u554f\u984c\u306b\u5bfe\u3059\u308b\u7b54\u3048\u306f\u4e0d\u660e\u77ad\u3067\u3042\u308b\u3002<br \/>\nlk-&amp;gt;locked=0\u304cpopcli\u95a2\u6570\u306e\u5f8c\u306b\u4e26\u3073\u66ff\u3048\u53ef\u80fd\u3060\u3063\u305f\u5834\u5408\u3001\u30ed\u30c3\u30af\u304c\u89e3\u653e\u3055\u308c\u308b\u524d\u306b\u4ed6\u306e\u30b9\u30ec\u30c3\u30c9\u3067\u5272\u308a\u8fbc\u307f\u304c\u6709\u52b9\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u306e\u3067\u3001acquire\u306f\u58ca\u308c\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u3002<br \/>\n\uff08\u3053\u3053\u7279\u306b\u8a33\u306b\u81ea\u4fe1\u306a\u3057\u3002\u539f\u6587: If lk-&gt;locked=0, were allowed to be re-ordered say after popcli, than acquire might break, because to another thread interrupts would be enabled before a lock is released.\uff09<br \/>\n\u30e1\u30e2\u30ea\u306e\u9806\u5e8f\u6027\u306b\u95a2\u3059\u308b\u4e0d\u660e\u77ad\u306a\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u7279\u6027\u306b\u983c\u308b\u306e\u3092\u907f\u3051\u308b\u305f\u3081\u3001xv6\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u304c\u4e26\u3073\u66ff\u3048\u3057\u306a\u3044\u4e8b\u3092\u4fdd\u8a3c\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044xchg\u547d\u4ee4\u3092\u4f7f\u3063\u3066\u3001\u30ea\u30b9\u30af\u3092\u72af\u3055\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u3002<\/p>\n<p>spinlock.c\u306erelease\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Release the lock.\r\nvoid\r\nrelease(struct spinlock *lk)\r\n{\r\n  if(!holding(lk))\r\n    panic(&quot;release&quot;);\r\n\r\n  lk-&gt;pcs&#x5B;0] = 0;\r\n  lk-&gt;cpu = 0;\r\n\r\n  \/\/ The xchg serializes, so that reads before release are \r\n  \/\/ not reordered after it.  The 1996 PentiumPro manual (Volume 3,\r\n  \/\/ 7.2) says reads can be carried out speculatively and in\r\n  \/\/ any order, which implies we need to serialize here.\r\n  \/\/ But the 2007 Intel 64 Architecture Memory Ordering White\r\n  \/\/ Paper says that Intel 64 and IA-32 will not move a load\r\n  \/\/ after a store. So lock-&gt;locked = 0 would work here.\r\n  \/\/ The xchg being asm volatile ensures gcc emits it after\r\n  \/\/ the above assignments (and after the critical section).\r\n  xchg(&amp;lk-&gt;locked, 0);\r\n\r\n  popcli();\r\n}<\/pre>\n<h3>\u611f\u60f3<\/h3>\n<p>\u30ed\u30c3\u30af\u6a5f\u69cb\u306f\u5c0f\u3055\u306a\u7a74\u3055\u3048\u7121\u3044\u3088\u3046\u614e\u91cd\u306b\u4f5c\u3089\u308c\u3066\u3044\u307e\u3059\u304c\u3001CPU\u306e\u30a2\u30a6\u30c8\u30aa\u30d6\u30aa\u30fc\u30c0\u30fc\u5b9f\u884c\u306e\u6a5f\u80fd\u306e\u5dee\u7570\u306b\u3088\u3063\u3066\u30ed\u30c3\u30af\u6a5f\u69cb\u306e\u4f5c\u308a\u65b9\u6b21\u7b2c\u3067\u306f\u7a74\u304c\u51fa\u6765\u3061\u3083\u3046\u3088\u3001\u3068\u3044\u3046\u8a71\u3067\u3059\u304b\u306d\u3002<\/p>\n<p>release\u95a2\u6570\u5185\u306e\u9577\u3044\u30b3\u30e1\u30f3\u30c8\u306b\u3082\u3088\u308a\u5177\u4f53\u7684\u306b\u4f3c\u305f\u3088\u3046\u306a\u4e8b\u304c\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e48\u301c49\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>\u3053\u306e\u7ae0\u3067\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u304c\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4e2d\u3067\u73fe\u308c\u308b\u9806\u756a\u3067\u4e00\u9023\u306e\u547d\u4ee4\u3092\u958b\u59cb\u3057\u5b8c\u4e86\u3059\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3002<br \/>\n\u3057\u304b\u3057\u306a\u304c\u3089\u591a\u304f\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u3001\u3088\u308a\u9ad8\u3044\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u9054\u6210\u3059\u308b\u305f\u3081\u306b\u547d\u4ee4\u3092\u9806\u756a\u901a\u308a\u306b\u306f\u5b9f\u884c\u3057\u306a\u3044\u3002\uff08\u30a2\u30a6\u30c8\u30aa\u30d6\u30aa\u30fc\u30c0\u30fc\u5b9f\u884c\uff09<br \/>\n\u3042\u308b\u547d\u4ee4\u304c\u3001\u305d\u306e\u5b8c\u4e86\u307e\u3067\u306b\u591a\u304f\u306e\u30b5\u30a4\u30af\u30eb\u3092\u8981\u3059\u308b\u5834\u5408\u3001\u4ed6\u306e\u547d\u4ee4\u3068\u30aa\u30fc\u30d0\u30fc\u30e9\u30c3\u30d7\u3057\u3066\u5b9f\u884c\u3057\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u30b9\u30c8\u30fc\u30eb\u3092\u907f\u308b\u305f\u3081\u306b\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u305d\u306e\u547d\u4ee4\u3092\u65e9\u3081\u306b\u767a\u884c\u3057\u305f\u3044\u306f\u305a\u3067\u3042\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u76f4\u5217\u306b\u4e26\u3093\u3067\u3044\u308b\u547d\u4ee4A\u3068\u547d\u4ee4B\u304c\u304a\u4e92\u3044\u306b\u4f9d\u5b58\u3057\u3066\u306a\u3044\u4e8b\u306b\u6c17\u3065\u304d\u3001\u547d\u4ee4A\u3092\u5b8c\u4e86\u3059\u308b\u3068\u304d\u306b\u547d\u4ee4B\u3082\u5b8c\u4e86\u3055\u305b\u308b\u305f\u3081\u306b\u3001\u547d\u4ee4A\u306e\u524d\u306b\u547d\u4ee4B\u3092\u958b\u59cb\u3059\u308b\u3002<br \/>\n\u3057\u304b\u3057\u306a\u304c\u3089\u540c\u6642\u5b9f\u884c\u306f\u3001\u547d\u4ee4\u306e\u4e26\u3073\u66ff\u3048\u306b\u3088\u3063\u3066\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u3001\u9593\u9055\u3063\u305f\u632f\u308b\u821e\u3044\u3092\u767a\u751f\u3055\u305b\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001release\u95a2\u6570\u304cxchg\u547d\u4ee4\u3092\u4f7f\u308f\u305a\u306blk-&amp;gt;locked\u306b0\u3092\u5272\u308a\u5f53\u3066\u308b\u5834\u5408\u3001\u4f55\u304c\u8d77\u304d\u308b\u306e\u304b\u5fc3\u914d\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3060\u308d\u3046\u3002<br \/>\nx86\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u3001\u4e16\u4ee3\u306b\u3088\u3063\u3066\u4fdd\u8a3c\u3059\u308b\u30e1\u30e2\u30ea\u306e\u9806\u5e8f\u6027\u306b\u9055\u3044\u304c\u3042\u308b\u305f\u3081\u3001\u3053\u306e\u554f\u984c\u306b\u5bfe\u3059\u308b\u7b54\u3048\u306f\u4e0d\u660e\u77ad\u3067\u3042\u308b\u3002<br \/>\nlk-&amp;gt;locked=0\u304cpopcli\u95a2\u6570\u306e\u5f8c\u306b\u4e26\u3073\u66ff\u3048\u53ef\u80fd\u3060\u3063\u305f\u5834\u5408\u3001\u30ed\u30c3\u30af\u304c\u89e3\u653e\u3055\u308c\u308b\u524d\u306b\u4ed6\u306e\u30b9\u30ec\u30c3\u30c9\u3067\u5272\u308a\u8fbc\u307f\u304c\u6709\u52b9\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u306e\u3067\u3001acquire\u306f\u58ca\u308c\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u3002<br \/>\n\uff08\u3053\u3053\u7279\u306b\u8a33\u306b\u81ea\u4fe1\u306a\u3057\u3002\u539f\u6587: If lk-&gt;locked=0, were allowed to be re-ordered say after popcli, than acquire might break, because to another thread interrupts would be enabled before a lock is released.\uff09<br \/>\n\u30e1\u30e2\u30ea\u306e\u9806\u5e8f\u6027\u306b\u95a2\u3059\u308b\u4e0d\u660e\u77ad\u306a\u30d7\u30ed\u30bb\u30c3\u30b5\u306e\u7279\u6027\u306b\u983c\u308b\u306e\u3092\u907f\u3051\u308b\u305f\u3081\u3001xv6\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u304c\u4e26\u3073\u66ff\u3048\u3057\u306a\u3044\u4e8b\u3092\u4fdd\u8a3c\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044xchg\u547d\u4ee4\u3092\u4f7f\u3063\u3066\u3001\u30ea\u30b9\u30af\u3092\u72af\u3055\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u3002<\/p>\n<p>spinlock.c\u306erelease\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Release the lock.\r\nvoid\r\nrelease(struct spinlock *lk)\r\n{\r\n  if(!holding(lk))\r\n    panic(&quot;release&quot;);\r\n\r\n  lk-&gt;pcs&#x5B;0] = 0;\r\n  lk-&gt;cpu = 0;\r\n\r\n  \/\/ The xchg serializes, so that reads before release are \r\n  \/\/ not reordered after it.  The 1996 PentiumPro manual (Volume 3,\r\n  \/\/ 7.2) says reads can be carried out speculatively and in\r\n  \/\/ any order, which implies we need to serialize here.\r\n  \/\/ But the 2007 Intel 64 Architecture Memory Ordering White\r\n  \/\/ Paper says that Intel 64 and IA-32 will not move a load\r\n  \/\/ after a store. So lock-&gt;locked = 0 would work here.\r\n  \/\/ The xchg being asm volatile ensures gcc emits it after\r\n  \/\/ the above assignments (and after the critical section).\r\n  xchg(&amp;lk-&gt;locked, 0);\r\n\r\n  popcli();\r\n}<\/pre>\n<h3>\u611f\u60f3<\/h3>\n<p>\u30ed\u30c3\u30af\u6a5f\u69cb\u306f\u5c0f\u3055\u306a\u7a74\u3055\u3048\u7121\u3044\u3088\u3046\u614e\u91cd\u306b\u4f5c\u3089\u308c\u3066\u3044\u307e\u3059\u304c\u3001CPU\u306e\u30a2\u30a6\u30c8\u30aa\u30d6\u30aa\u30fc\u30c0\u30fc\u5b9f\u884c\u306e\u6a5f\u80fd\u306e\u5dee\u7570\u306b\u3088\u3063\u3066\u30ed\u30c3\u30af\u6a5f\u69cb\u306e\u4f5c\u308a\u65b9\u6b21\u7b2c\u3067\u306f\u7a74\u304c\u51fa\u6765\u3061\u3083\u3046\u3088\u3001\u3068\u3044\u3046\u8a71\u3067\u3059\u304b\u306d\u3002<\/p>\n<p>release\u95a2\u6570\u5185\u306e\u9577\u3044\u30b3\u30e1\u30f3\u30c8\u306b\u3082\u3088\u308a\u5177\u4f53\u7684\u306b\u4f3c\u305f\u3088\u3046\u306a\u4e8b\u304c\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u306d\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-1441","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\/1441","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=1441"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1441\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=1441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=1441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=1441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}