ldt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
  4. * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
  5. * Copyright (C) 2002 Andi Kleen
  6. *
  7. * This handles calls from both 32bit and 64bit mode.
  8. *
  9. * Lock order:
  10. * contex.ldt_usr_sem
  11. * mmap_lock
  12. * context.lock
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/gfp.h>
  16. #include <linux/sched.h>
  17. #include <linux/string.h>
  18. #include <linux/mm.h>
  19. #include <linux/smp.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/slab.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/ldt.h>
  25. #include <asm/tlb.h>
  26. #include <asm/desc.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/pgtable_areas.h>
  29. #include <xen/xen.h>
  30. /* This is a multiple of PAGE_SIZE. */
  31. #define LDT_SLOT_STRIDE (LDT_ENTRIES * LDT_ENTRY_SIZE)
  32. static inline void *ldt_slot_va(int slot)
  33. {
  34. return (void *)(LDT_BASE_ADDR + LDT_SLOT_STRIDE * slot);
  35. }
  36. void load_mm_ldt(struct mm_struct *mm)
  37. {
  38. struct ldt_struct *ldt;
  39. /* READ_ONCE synchronizes with smp_store_release */
  40. ldt = READ_ONCE(mm->context.ldt);
  41. /*
  42. * Any change to mm->context.ldt is followed by an IPI to all
  43. * CPUs with the mm active. The LDT will not be freed until
  44. * after the IPI is handled by all such CPUs. This means that,
  45. * if the ldt_struct changes before we return, the values we see
  46. * will be safe, and the new values will be loaded before we run
  47. * any user code.
  48. *
  49. * NB: don't try to convert this to use RCU without extreme care.
  50. * We would still need IRQs off, because we don't want to change
  51. * the local LDT after an IPI loaded a newer value than the one
  52. * that we can see.
  53. */
  54. if (unlikely(ldt)) {
  55. if (static_cpu_has(X86_FEATURE_PTI)) {
  56. if (WARN_ON_ONCE((unsigned long)ldt->slot > 1)) {
  57. /*
  58. * Whoops -- either the new LDT isn't mapped
  59. * (if slot == -1) or is mapped into a bogus
  60. * slot (if slot > 1).
  61. */
  62. clear_LDT();
  63. return;
  64. }
  65. /*
  66. * If page table isolation is enabled, ldt->entries
  67. * will not be mapped in the userspace pagetables.
  68. * Tell the CPU to access the LDT through the alias
  69. * at ldt_slot_va(ldt->slot).
  70. */
  71. set_ldt(ldt_slot_va(ldt->slot), ldt->nr_entries);
  72. } else {
  73. set_ldt(ldt->entries, ldt->nr_entries);
  74. }
  75. } else {
  76. clear_LDT();
  77. }
  78. }
  79. void switch_ldt(struct mm_struct *prev, struct mm_struct *next)
  80. {
  81. /*
  82. * Load the LDT if either the old or new mm had an LDT.
  83. *
  84. * An mm will never go from having an LDT to not having an LDT. Two
  85. * mms never share an LDT, so we don't gain anything by checking to
  86. * see whether the LDT changed. There's also no guarantee that
  87. * prev->context.ldt actually matches LDTR, but, if LDTR is non-NULL,
  88. * then prev->context.ldt will also be non-NULL.
  89. *
  90. * If we really cared, we could optimize the case where prev == next
  91. * and we're exiting lazy mode. Most of the time, if this happens,
  92. * we don't actually need to reload LDTR, but modify_ldt() is mostly
  93. * used by legacy code and emulators where we don't need this level of
  94. * performance.
  95. *
  96. * This uses | instead of || because it generates better code.
  97. */
  98. if (unlikely((unsigned long)prev->context.ldt |
  99. (unsigned long)next->context.ldt))
  100. load_mm_ldt(next);
  101. DEBUG_LOCKS_WARN_ON(preemptible());
  102. }
  103. static void refresh_ldt_segments(void)
  104. {
  105. #ifdef CONFIG_X86_64
  106. unsigned short sel;
  107. /*
  108. * Make sure that the cached DS and ES descriptors match the updated
  109. * LDT.
  110. */
  111. savesegment(ds, sel);
  112. if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
  113. loadsegment(ds, sel);
  114. savesegment(es, sel);
  115. if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
  116. loadsegment(es, sel);
  117. #endif
  118. }
  119. /* context.lock is held by the task which issued the smp function call */
  120. static void flush_ldt(void *__mm)
  121. {
  122. struct mm_struct *mm = __mm;
  123. if (this_cpu_read(cpu_tlbstate.loaded_mm) != mm)
  124. return;
  125. load_mm_ldt(mm);
  126. refresh_ldt_segments();
  127. }
  128. /* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
  129. static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
  130. {
  131. struct ldt_struct *new_ldt;
  132. unsigned int alloc_size;
  133. if (num_entries > LDT_ENTRIES)
  134. return NULL;
  135. new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
  136. if (!new_ldt)
  137. return NULL;
  138. BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
  139. alloc_size = num_entries * LDT_ENTRY_SIZE;
  140. /*
  141. * Xen is very picky: it requires a page-aligned LDT that has no
  142. * trailing nonzero bytes in any page that contains LDT descriptors.
  143. * Keep it simple: zero the whole allocation and never allocate less
  144. * than PAGE_SIZE.
  145. */
  146. if (alloc_size > PAGE_SIZE)
  147. new_ldt->entries = vzalloc(alloc_size);
  148. else
  149. new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
  150. if (!new_ldt->entries) {
  151. kfree(new_ldt);
  152. return NULL;
  153. }
  154. /* The new LDT isn't aliased for PTI yet. */
  155. new_ldt->slot = -1;
  156. new_ldt->nr_entries = num_entries;
  157. return new_ldt;
  158. }
  159. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  160. static void do_sanity_check(struct mm_struct *mm,
  161. bool had_kernel_mapping,
  162. bool had_user_mapping)
  163. {
  164. if (mm->context.ldt) {
  165. /*
  166. * We already had an LDT. The top-level entry should already
  167. * have been allocated and synchronized with the usermode
  168. * tables.
  169. */
  170. WARN_ON(!had_kernel_mapping);
  171. if (boot_cpu_has(X86_FEATURE_PTI))
  172. WARN_ON(!had_user_mapping);
  173. } else {
  174. /*
  175. * This is the first time we're mapping an LDT for this process.
  176. * Sync the pgd to the usermode tables.
  177. */
  178. WARN_ON(had_kernel_mapping);
  179. if (boot_cpu_has(X86_FEATURE_PTI))
  180. WARN_ON(had_user_mapping);
  181. }
  182. }
  183. #ifdef CONFIG_X86_PAE
  184. static pmd_t *pgd_to_pmd_walk(pgd_t *pgd, unsigned long va)
  185. {
  186. p4d_t *p4d;
  187. pud_t *pud;
  188. if (pgd->pgd == 0)
  189. return NULL;
  190. p4d = p4d_offset(pgd, va);
  191. if (p4d_none(*p4d))
  192. return NULL;
  193. pud = pud_offset(p4d, va);
  194. if (pud_none(*pud))
  195. return NULL;
  196. return pmd_offset(pud, va);
  197. }
  198. static void map_ldt_struct_to_user(struct mm_struct *mm)
  199. {
  200. pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR);
  201. pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
  202. pmd_t *k_pmd, *u_pmd;
  203. k_pmd = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR);
  204. u_pmd = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR);
  205. if (boot_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt)
  206. set_pmd(u_pmd, *k_pmd);
  207. }
  208. static void sanity_check_ldt_mapping(struct mm_struct *mm)
  209. {
  210. pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR);
  211. pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
  212. bool had_kernel, had_user;
  213. pmd_t *k_pmd, *u_pmd;
  214. k_pmd = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR);
  215. u_pmd = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR);
  216. had_kernel = (k_pmd->pmd != 0);
  217. had_user = (u_pmd->pmd != 0);
  218. do_sanity_check(mm, had_kernel, had_user);
  219. }
  220. #else /* !CONFIG_X86_PAE */
  221. static void map_ldt_struct_to_user(struct mm_struct *mm)
  222. {
  223. pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR);
  224. if (boot_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt)
  225. set_pgd(kernel_to_user_pgdp(pgd), *pgd);
  226. }
  227. static void sanity_check_ldt_mapping(struct mm_struct *mm)
  228. {
  229. pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR);
  230. bool had_kernel = (pgd->pgd != 0);
  231. bool had_user = (kernel_to_user_pgdp(pgd)->pgd != 0);
  232. do_sanity_check(mm, had_kernel, had_user);
  233. }
  234. #endif /* CONFIG_X86_PAE */
  235. /*
  236. * If PTI is enabled, this maps the LDT into the kernelmode and
  237. * usermode tables for the given mm.
  238. */
  239. static int
  240. map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
  241. {
  242. unsigned long va;
  243. bool is_vmalloc;
  244. spinlock_t *ptl;
  245. int i, nr_pages;
  246. if (!boot_cpu_has(X86_FEATURE_PTI))
  247. return 0;
  248. /*
  249. * Any given ldt_struct should have map_ldt_struct() called at most
  250. * once.
  251. */
  252. WARN_ON(ldt->slot != -1);
  253. /* Check if the current mappings are sane */
  254. sanity_check_ldt_mapping(mm);
  255. is_vmalloc = is_vmalloc_addr(ldt->entries);
  256. nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE);
  257. for (i = 0; i < nr_pages; i++) {
  258. unsigned long offset = i << PAGE_SHIFT;
  259. const void *src = (char *)ldt->entries + offset;
  260. unsigned long pfn;
  261. pgprot_t pte_prot;
  262. pte_t pte, *ptep;
  263. va = (unsigned long)ldt_slot_va(slot) + offset;
  264. pfn = is_vmalloc ? vmalloc_to_pfn(src) :
  265. page_to_pfn(virt_to_page(src));
  266. /*
  267. * Treat the PTI LDT range as a *userspace* range.
  268. * get_locked_pte() will allocate all needed pagetables
  269. * and account for them in this mm.
  270. */
  271. ptep = get_locked_pte(mm, va, &ptl);
  272. if (!ptep)
  273. return -ENOMEM;
  274. /*
  275. * Map it RO so the easy to find address is not a primary
  276. * target via some kernel interface which misses a
  277. * permission check.
  278. */
  279. pte_prot = __pgprot(__PAGE_KERNEL_RO & ~_PAGE_GLOBAL);
  280. /* Filter out unsuppored __PAGE_KERNEL* bits: */
  281. pgprot_val(pte_prot) &= __supported_pte_mask;
  282. pte = pfn_pte(pfn, pte_prot);
  283. set_pte_at(mm, va, ptep, pte);
  284. pte_unmap_unlock(ptep, ptl);
  285. }
  286. /* Propagate LDT mapping to the user page-table */
  287. map_ldt_struct_to_user(mm);
  288. ldt->slot = slot;
  289. return 0;
  290. }
  291. static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
  292. {
  293. unsigned long va;
  294. int i, nr_pages;
  295. if (!ldt)
  296. return;
  297. /* LDT map/unmap is only required for PTI */
  298. if (!boot_cpu_has(X86_FEATURE_PTI))
  299. return;
  300. nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE);
  301. for (i = 0; i < nr_pages; i++) {
  302. unsigned long offset = i << PAGE_SHIFT;
  303. spinlock_t *ptl;
  304. pte_t *ptep;
  305. va = (unsigned long)ldt_slot_va(ldt->slot) + offset;
  306. ptep = get_locked_pte(mm, va, &ptl);
  307. pte_clear(mm, va, ptep);
  308. pte_unmap_unlock(ptep, ptl);
  309. }
  310. va = (unsigned long)ldt_slot_va(ldt->slot);
  311. flush_tlb_mm_range(mm, va, va + nr_pages * PAGE_SIZE, PAGE_SHIFT, false);
  312. }
  313. #else /* !CONFIG_PAGE_TABLE_ISOLATION */
  314. static int
  315. map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
  316. {
  317. return 0;
  318. }
  319. static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
  320. {
  321. }
  322. #endif /* CONFIG_PAGE_TABLE_ISOLATION */
  323. static void free_ldt_pgtables(struct mm_struct *mm)
  324. {
  325. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  326. struct mmu_gather tlb;
  327. unsigned long start = LDT_BASE_ADDR;
  328. unsigned long end = LDT_END_ADDR;
  329. if (!boot_cpu_has(X86_FEATURE_PTI))
  330. return;
  331. tlb_gather_mmu(&tlb, mm, start, end);
  332. free_pgd_range(&tlb, start, end, start, end);
  333. tlb_finish_mmu(&tlb, start, end);
  334. #endif
  335. }
  336. /* After calling this, the LDT is immutable. */
  337. static void finalize_ldt_struct(struct ldt_struct *ldt)
  338. {
  339. paravirt_alloc_ldt(ldt->entries, ldt->nr_entries);
  340. }
  341. static void install_ldt(struct mm_struct *mm, struct ldt_struct *ldt)
  342. {
  343. mutex_lock(&mm->context.lock);
  344. /* Synchronizes with READ_ONCE in load_mm_ldt. */
  345. smp_store_release(&mm->context.ldt, ldt);
  346. /* Activate the LDT for all CPUs using currents mm. */
  347. on_each_cpu_mask(mm_cpumask(mm), flush_ldt, mm, true);
  348. mutex_unlock(&mm->context.lock);
  349. }
  350. static void free_ldt_struct(struct ldt_struct *ldt)
  351. {
  352. if (likely(!ldt))
  353. return;
  354. paravirt_free_ldt(ldt->entries, ldt->nr_entries);
  355. if (ldt->nr_entries * LDT_ENTRY_SIZE > PAGE_SIZE)
  356. vfree_atomic(ldt->entries);
  357. else
  358. free_page((unsigned long)ldt->entries);
  359. kfree(ldt);
  360. }
  361. /*
  362. * Called on fork from arch_dup_mmap(). Just copy the current LDT state,
  363. * the new task is not running, so nothing can be installed.
  364. */
  365. int ldt_dup_context(struct mm_struct *old_mm, struct mm_struct *mm)
  366. {
  367. struct ldt_struct *new_ldt;
  368. int retval = 0;
  369. if (!old_mm)
  370. return 0;
  371. mutex_lock(&old_mm->context.lock);
  372. if (!old_mm->context.ldt)
  373. goto out_unlock;
  374. new_ldt = alloc_ldt_struct(old_mm->context.ldt->nr_entries);
  375. if (!new_ldt) {
  376. retval = -ENOMEM;
  377. goto out_unlock;
  378. }
  379. memcpy(new_ldt->entries, old_mm->context.ldt->entries,
  380. new_ldt->nr_entries * LDT_ENTRY_SIZE);
  381. finalize_ldt_struct(new_ldt);
  382. retval = map_ldt_struct(mm, new_ldt, 0);
  383. if (retval) {
  384. free_ldt_pgtables(mm);
  385. free_ldt_struct(new_ldt);
  386. goto out_unlock;
  387. }
  388. mm->context.ldt = new_ldt;
  389. out_unlock:
  390. mutex_unlock(&old_mm->context.lock);
  391. return retval;
  392. }
  393. /*
  394. * No need to lock the MM as we are the last user
  395. *
  396. * 64bit: Don't touch the LDT register - we're already in the next thread.
  397. */
  398. void destroy_context_ldt(struct mm_struct *mm)
  399. {
  400. free_ldt_struct(mm->context.ldt);
  401. mm->context.ldt = NULL;
  402. }
  403. void ldt_arch_exit_mmap(struct mm_struct *mm)
  404. {
  405. free_ldt_pgtables(mm);
  406. }
  407. static int read_ldt(void __user *ptr, unsigned long bytecount)
  408. {
  409. struct mm_struct *mm = current->mm;
  410. unsigned long entries_size;
  411. int retval;
  412. down_read(&mm->context.ldt_usr_sem);
  413. if (!mm->context.ldt) {
  414. retval = 0;
  415. goto out_unlock;
  416. }
  417. if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
  418. bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
  419. entries_size = mm->context.ldt->nr_entries * LDT_ENTRY_SIZE;
  420. if (entries_size > bytecount)
  421. entries_size = bytecount;
  422. if (copy_to_user(ptr, mm->context.ldt->entries, entries_size)) {
  423. retval = -EFAULT;
  424. goto out_unlock;
  425. }
  426. if (entries_size != bytecount) {
  427. /* Zero-fill the rest and pretend we read bytecount bytes. */
  428. if (clear_user(ptr + entries_size, bytecount - entries_size)) {
  429. retval = -EFAULT;
  430. goto out_unlock;
  431. }
  432. }
  433. retval = bytecount;
  434. out_unlock:
  435. up_read(&mm->context.ldt_usr_sem);
  436. return retval;
  437. }
  438. static int read_default_ldt(void __user *ptr, unsigned long bytecount)
  439. {
  440. /* CHECKME: Can we use _one_ random number ? */
  441. #ifdef CONFIG_X86_32
  442. unsigned long size = 5 * sizeof(struct desc_struct);
  443. #else
  444. unsigned long size = 128;
  445. #endif
  446. if (bytecount > size)
  447. bytecount = size;
  448. if (clear_user(ptr, bytecount))
  449. return -EFAULT;
  450. return bytecount;
  451. }
  452. static bool allow_16bit_segments(void)
  453. {
  454. if (!IS_ENABLED(CONFIG_X86_16BIT))
  455. return false;
  456. #ifdef CONFIG_XEN_PV
  457. /*
  458. * Xen PV does not implement ESPFIX64, which means that 16-bit
  459. * segments will not work correctly. Until either Xen PV implements
  460. * ESPFIX64 and can signal this fact to the guest or unless someone
  461. * provides compelling evidence that allowing broken 16-bit segments
  462. * is worthwhile, disallow 16-bit segments under Xen PV.
  463. */
  464. if (xen_pv_domain()) {
  465. pr_info_once("Warning: 16-bit segments do not work correctly in a Xen PV guest\n");
  466. return false;
  467. }
  468. #endif
  469. return true;
  470. }
  471. static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
  472. {
  473. struct mm_struct *mm = current->mm;
  474. struct ldt_struct *new_ldt, *old_ldt;
  475. unsigned int old_nr_entries, new_nr_entries;
  476. struct user_desc ldt_info;
  477. struct desc_struct ldt;
  478. int error;
  479. error = -EINVAL;
  480. if (bytecount != sizeof(ldt_info))
  481. goto out;
  482. error = -EFAULT;
  483. if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  484. goto out;
  485. error = -EINVAL;
  486. if (ldt_info.entry_number >= LDT_ENTRIES)
  487. goto out;
  488. if (ldt_info.contents == 3) {
  489. if (oldmode)
  490. goto out;
  491. if (ldt_info.seg_not_present == 0)
  492. goto out;
  493. }
  494. if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
  495. LDT_empty(&ldt_info)) {
  496. /* The user wants to clear the entry. */
  497. memset(&ldt, 0, sizeof(ldt));
  498. } else {
  499. if (!ldt_info.seg_32bit && !allow_16bit_segments()) {
  500. error = -EINVAL;
  501. goto out;
  502. }
  503. fill_ldt(&ldt, &ldt_info);
  504. if (oldmode)
  505. ldt.avl = 0;
  506. }
  507. if (down_write_killable(&mm->context.ldt_usr_sem))
  508. return -EINTR;
  509. old_ldt = mm->context.ldt;
  510. old_nr_entries = old_ldt ? old_ldt->nr_entries : 0;
  511. new_nr_entries = max(ldt_info.entry_number + 1, old_nr_entries);
  512. error = -ENOMEM;
  513. new_ldt = alloc_ldt_struct(new_nr_entries);
  514. if (!new_ldt)
  515. goto out_unlock;
  516. if (old_ldt)
  517. memcpy(new_ldt->entries, old_ldt->entries, old_nr_entries * LDT_ENTRY_SIZE);
  518. new_ldt->entries[ldt_info.entry_number] = ldt;
  519. finalize_ldt_struct(new_ldt);
  520. /*
  521. * If we are using PTI, map the new LDT into the userspace pagetables.
  522. * If there is already an LDT, use the other slot so that other CPUs
  523. * will continue to use the old LDT until install_ldt() switches
  524. * them over to the new LDT.
  525. */
  526. error = map_ldt_struct(mm, new_ldt, old_ldt ? !old_ldt->slot : 0);
  527. if (error) {
  528. /*
  529. * This only can fail for the first LDT setup. If an LDT is
  530. * already installed then the PTE page is already
  531. * populated. Mop up a half populated page table.
  532. */
  533. if (!WARN_ON_ONCE(old_ldt))
  534. free_ldt_pgtables(mm);
  535. free_ldt_struct(new_ldt);
  536. goto out_unlock;
  537. }
  538. install_ldt(mm, new_ldt);
  539. unmap_ldt_struct(mm, old_ldt);
  540. free_ldt_struct(old_ldt);
  541. error = 0;
  542. out_unlock:
  543. up_write(&mm->context.ldt_usr_sem);
  544. out:
  545. return error;
  546. }
  547. SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
  548. unsigned long , bytecount)
  549. {
  550. int ret = -ENOSYS;
  551. switch (func) {
  552. case 0:
  553. ret = read_ldt(ptr, bytecount);
  554. break;
  555. case 1:
  556. ret = write_ldt(ptr, bytecount, 1);
  557. break;
  558. case 2:
  559. ret = read_default_ldt(ptr, bytecount);
  560. break;
  561. case 0x11:
  562. ret = write_ldt(ptr, bytecount, 0);
  563. break;
  564. }
  565. /*
  566. * The SYSCALL_DEFINE() macros give us an 'unsigned long'
  567. * return type, but tht ABI for sys_modify_ldt() expects
  568. * 'int'. This cast gives us an int-sized value in %rax
  569. * for the return code. The 'unsigned' is necessary so
  570. * the compiler does not try to sign-extend the negative
  571. * return codes into the high half of the register when
  572. * taking the value from int->long.
  573. */
  574. return (unsigned int)ret;
  575. }