Support LDT modification on Mac OS.

Change-Id: I292d87408ea4af4c146d5f546b319edcafc9b323
diff --git a/src/thread_x86.cc b/src/thread_x86.cc
index 73b0465..30d19d4 100644
--- a/src/thread_x86.cc
+++ b/src/thread_x86.cc
@@ -26,6 +26,13 @@
 #if defined(__APPLE__)
 #include <architecture/i386/table.h>
 #include <i386/user_ldt.h>
+struct descriptor_table_entry_t {
+  uint16_t limit0;
+  uint16_t base0;
+  unsigned base1: 8, type: 4, s: 1, dpl: 2, p: 1;
+  unsigned limit: 4, avl: 1, l: 1, d: 1, g: 1, base2: 8;
+} __attribute__((packed));
+#define MODIFY_LDT_CONTENTS_DATA 0
 #else
 #include <asm/ldt.h>
 #endif
@@ -33,53 +40,88 @@
 namespace art {
 
 void Thread::InitCpu() {
-#if defined(__APPLE__)
-  UNIMPLEMENTED(WARNING);
-#else
   static Mutex modify_ldt_lock("modify_ldt lock");
   MutexLock mu(modify_ldt_lock);
 
-  // Read LDT
+  const uintptr_t base = reinterpret_cast<uintptr_t>(this);
+  const size_t limit = kPageSize;
+
+  const int contents = MODIFY_LDT_CONTENTS_DATA;
+  const int seg_32bit = 1;
+  const int read_exec_only = 0;
+  const int limit_in_pages = 0;
+  const int seg_not_present = 0;
+  const int useable = 1;
+
+  int entry_number = -1;
+
+#if defined(__APPLE__)
+  descriptor_table_entry_t entry;
+  memset(&entry, 0, sizeof(entry));
+  entry.limit0 = (limit & 0x0ffff);
+  entry.limit  = (limit & 0xf0000) >> 16;
+  entry.base0 = (base & 0x0000ffff);
+  entry.base1 = (base & 0x00ff0000) >> 16;
+  entry.base2 = (base & 0xff000000) >> 24;
+  entry.type = ((read_exec_only ^ 1) << 1) | (contents << 2);
+  entry.s = 1;
+  entry.dpl = 0x3;
+  entry.p = seg_not_present ^ 1;
+  entry.avl = useable;
+  entry.l = 0;
+  entry.d = seg_32bit;
+  entry.g = limit_in_pages;
+
+  entry_number = i386_set_ldt(LDT_AUTO_ALLOC, (ldt_entry*)(void*)(&entry), 1);
+  if (entry_number == -1) {
+    PLOG(FATAL) << "i386_set_ldt failed";
+  }
+#else
+  // Read current LDT entries.
   CHECK_EQ((size_t)LDT_ENTRY_SIZE, sizeof(uint64_t));
   std::vector<uint64_t> ldt(LDT_ENTRIES);
   size_t ldt_size(sizeof(uint64_t) * ldt.size());
   memset(&ldt[0], 0, ldt_size);
+  // TODO: why doesn't this return LDT_ENTRY_SIZE * LDT_ENTRIES for the main thread?
   syscall(__NR_modify_ldt, 0, &ldt[0], ldt_size);
-  // Create empty slot to point at current Thread*
+
+  // Find the first empty slot.
+  for (entry_number = 0; entry_number < LDT_ENTRIES && ldt[entry_number] != 0; ++entry_number) {
+  }
+  if (entry_number >= LDT_ENTRIES) {
+    LOG(FATAL) << "Failed to find a free LDT slot";
+  }
+
+  // Update LDT entry.
   user_desc ldt_entry;
   memset(&ldt_entry, 0, sizeof(ldt_entry));
-  ldt_entry.entry_number = -1;
-  ldt_entry.base_addr = (unsigned int)this;
-  ldt_entry.limit = kPageSize;
-  ldt_entry.seg_32bit = 1;
-  ldt_entry.contents = MODIFY_LDT_CONTENTS_DATA;
-  ldt_entry.read_exec_only = 0;
-  ldt_entry.limit_in_pages = 0;
-  ldt_entry.seg_not_present = 0;
-  ldt_entry.useable = 1;
-  for (int i = 0; i < LDT_ENTRIES; i++) {
-    if (ldt[i] == 0) {
-      ldt_entry.entry_number = i;
-      break;
-    }
-  }
-  if (ldt_entry.entry_number >= LDT_ENTRIES) {
-    LOG(FATAL) << "Failed to find available LDT slot";
-  }
-  // Update LDT
+  ldt_entry.entry_number = entry_number;
+  ldt_entry.base_addr = base;
+  ldt_entry.limit = limit;
+  ldt_entry.seg_32bit = seg_32bit;
+  ldt_entry.contents = contents;
+  ldt_entry.read_exec_only = read_exec_only;
+  ldt_entry.limit_in_pages = limit_in_pages;
+  ldt_entry.seg_not_present = seg_not_present;
+  ldt_entry.useable = useable;
   CHECK_EQ(0, syscall(__NR_modify_ldt, 1, &ldt_entry, sizeof(ldt_entry)));
-  // Change FS to be new LDT entry
+  entry_number = ldt_entry.entry_number;
+#endif
+
+  // Change %fs to be new LDT entry.
   uint16_t table_indicator = 1 << 2;  // LDT
   uint16_t rpl = 3;  // Requested privilege level
-  uint16_t selector = (ldt_entry.entry_number << 3) | table_indicator | rpl;
+  uint16_t selector = (entry_number << 3) | table_indicator | rpl;
   // TODO: use our assembler to generate code
   asm volatile("movw %w0, %%fs"
       :    // output
       : "q"(selector)  // input
       :);  // clobber
-  // Allow easy indirection back to Thread*
+
+  // Allow easy indirection back to Thread*.
   self_ = this;
-  // Sanity check reads from FS goes to this Thread*
+
+  // Sanity check that reads from %fs point to this Thread*.
   Thread* self_check;
   // TODO: use our assembler to generate code
   CHECK_EQ(THREAD_SELF_OFFSET, OFFSETOF_MEMBER(Thread, self_));
@@ -88,7 +130,6 @@
       : "r"(THREAD_SELF_OFFSET)  // input
       :);  // clobber
   CHECK_EQ(self_check, this);
-#endif
 }
 
 }  // namespace art