ldt.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/sched.h>
  7. #include <linux/slab.h>
  8. #include <linux/syscalls.h>
  9. #include <linux/uaccess.h>
  10. #include <asm/unistd.h>
  11. #include <os.h>
  12. #include <skas.h>
  13. #include <sysdep/tls.h>
  14. static inline int modify_ldt (int func, void *ptr, unsigned long bytecount)
  15. {
  16. return syscall(__NR_modify_ldt, func, ptr, bytecount);
  17. }
  18. static long write_ldt_entry(struct mm_id *mm_idp, int func,
  19. struct user_desc *desc, void **addr, int done)
  20. {
  21. long res;
  22. void *stub_addr;
  23. res = syscall_stub_data(mm_idp, (unsigned long *)desc,
  24. (sizeof(*desc) + sizeof(long) - 1) &
  25. ~(sizeof(long) - 1),
  26. addr, &stub_addr);
  27. if (!res) {
  28. unsigned long args[] = { func,
  29. (unsigned long)stub_addr,
  30. sizeof(*desc),
  31. 0, 0, 0 };
  32. res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
  33. 0, addr, done);
  34. }
  35. return res;
  36. }
  37. /*
  38. * In skas mode, we hold our own ldt data in UML.
  39. * Thus, the code implementing sys_modify_ldt_skas
  40. * is very similar to (and mostly stolen from) sys_modify_ldt
  41. * for arch/i386/kernel/ldt.c
  42. * The routines copied and modified in part are:
  43. * - read_ldt
  44. * - read_default_ldt
  45. * - write_ldt
  46. * - sys_modify_ldt_skas
  47. */
  48. static int read_ldt(void __user * ptr, unsigned long bytecount)
  49. {
  50. int i, err = 0;
  51. unsigned long size;
  52. uml_ldt_t *ldt = &current->mm->context.arch.ldt;
  53. if (!ldt->entry_count)
  54. goto out;
  55. if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
  56. bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
  57. err = bytecount;
  58. mutex_lock(&ldt->lock);
  59. if (ldt->entry_count <= LDT_DIRECT_ENTRIES) {
  60. size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
  61. if (size > bytecount)
  62. size = bytecount;
  63. if (copy_to_user(ptr, ldt->u.entries, size))
  64. err = -EFAULT;
  65. bytecount -= size;
  66. ptr += size;
  67. }
  68. else {
  69. for (i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
  70. i++) {
  71. size = PAGE_SIZE;
  72. if (size > bytecount)
  73. size = bytecount;
  74. if (copy_to_user(ptr, ldt->u.pages[i], size)) {
  75. err = -EFAULT;
  76. break;
  77. }
  78. bytecount -= size;
  79. ptr += size;
  80. }
  81. }
  82. mutex_unlock(&ldt->lock);
  83. if (bytecount == 0 || err == -EFAULT)
  84. goto out;
  85. if (clear_user(ptr, bytecount))
  86. err = -EFAULT;
  87. out:
  88. return err;
  89. }
  90. static int read_default_ldt(void __user * ptr, unsigned long bytecount)
  91. {
  92. int err;
  93. if (bytecount > 5*LDT_ENTRY_SIZE)
  94. bytecount = 5*LDT_ENTRY_SIZE;
  95. err = bytecount;
  96. /*
  97. * UML doesn't support lcall7 and lcall27.
  98. * So, we don't really have a default ldt, but emulate
  99. * an empty ldt of common host default ldt size.
  100. */
  101. if (clear_user(ptr, bytecount))
  102. err = -EFAULT;
  103. return err;
  104. }
  105. static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
  106. {
  107. uml_ldt_t *ldt = &current->mm->context.arch.ldt;
  108. struct mm_id * mm_idp = &current->mm->context.id;
  109. int i, err;
  110. struct user_desc ldt_info;
  111. struct ldt_entry entry0, *ldt_p;
  112. void *addr = NULL;
  113. err = -EINVAL;
  114. if (bytecount != sizeof(ldt_info))
  115. goto out;
  116. err = -EFAULT;
  117. if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  118. goto out;
  119. err = -EINVAL;
  120. if (ldt_info.entry_number >= LDT_ENTRIES)
  121. goto out;
  122. if (ldt_info.contents == 3) {
  123. if (func == 1)
  124. goto out;
  125. if (ldt_info.seg_not_present == 0)
  126. goto out;
  127. }
  128. mutex_lock(&ldt->lock);
  129. err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
  130. if (err)
  131. goto out_unlock;
  132. if (ldt_info.entry_number >= ldt->entry_count &&
  133. ldt_info.entry_number >= LDT_DIRECT_ENTRIES) {
  134. for (i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
  135. i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
  136. i++) {
  137. if (i == 0)
  138. memcpy(&entry0, ldt->u.entries,
  139. sizeof(entry0));
  140. ldt->u.pages[i] = (struct ldt_entry *)
  141. __get_free_page(GFP_KERNEL|__GFP_ZERO);
  142. if (!ldt->u.pages[i]) {
  143. err = -ENOMEM;
  144. /* Undo the change in host */
  145. memset(&ldt_info, 0, sizeof(ldt_info));
  146. write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
  147. goto out_unlock;
  148. }
  149. if (i == 0) {
  150. memcpy(ldt->u.pages[0], &entry0,
  151. sizeof(entry0));
  152. memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
  153. sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
  154. }
  155. ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
  156. }
  157. }
  158. if (ldt->entry_count <= ldt_info.entry_number)
  159. ldt->entry_count = ldt_info.entry_number + 1;
  160. if (ldt->entry_count <= LDT_DIRECT_ENTRIES)
  161. ldt_p = ldt->u.entries + ldt_info.entry_number;
  162. else
  163. ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
  164. ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
  165. if (ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
  166. (func == 1 || LDT_empty(&ldt_info))) {
  167. ldt_p->a = 0;
  168. ldt_p->b = 0;
  169. }
  170. else{
  171. if (func == 1)
  172. ldt_info.useable = 0;
  173. ldt_p->a = LDT_entry_a(&ldt_info);
  174. ldt_p->b = LDT_entry_b(&ldt_info);
  175. }
  176. err = 0;
  177. out_unlock:
  178. mutex_unlock(&ldt->lock);
  179. out:
  180. return err;
  181. }
  182. static long do_modify_ldt_skas(int func, void __user *ptr,
  183. unsigned long bytecount)
  184. {
  185. int ret = -ENOSYS;
  186. switch (func) {
  187. case 0:
  188. ret = read_ldt(ptr, bytecount);
  189. break;
  190. case 1:
  191. case 0x11:
  192. ret = write_ldt(ptr, bytecount, func);
  193. break;
  194. case 2:
  195. ret = read_default_ldt(ptr, bytecount);
  196. break;
  197. }
  198. return ret;
  199. }
  200. static DEFINE_SPINLOCK(host_ldt_lock);
  201. static short dummy_list[9] = {0, -1};
  202. static short * host_ldt_entries = NULL;
  203. static void ldt_get_host_info(void)
  204. {
  205. long ret;
  206. struct ldt_entry * ldt;
  207. short *tmp;
  208. int i, size, k, order;
  209. spin_lock(&host_ldt_lock);
  210. if (host_ldt_entries != NULL) {
  211. spin_unlock(&host_ldt_lock);
  212. return;
  213. }
  214. host_ldt_entries = dummy_list+1;
  215. spin_unlock(&host_ldt_lock);
  216. for (i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++)
  217. ;
  218. ldt = (struct ldt_entry *)
  219. __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
  220. if (ldt == NULL) {
  221. printk(KERN_ERR "ldt_get_host_info: couldn't allocate buffer "
  222. "for host ldt\n");
  223. return;
  224. }
  225. ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
  226. if (ret < 0) {
  227. printk(KERN_ERR "ldt_get_host_info: couldn't read host ldt\n");
  228. goto out_free;
  229. }
  230. if (ret == 0) {
  231. /* default_ldt is active, simply write an empty entry 0 */
  232. host_ldt_entries = dummy_list;
  233. goto out_free;
  234. }
  235. for (i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++) {
  236. if (ldt[i].a != 0 || ldt[i].b != 0)
  237. size++;
  238. }
  239. if (size < ARRAY_SIZE(dummy_list))
  240. host_ldt_entries = dummy_list;
  241. else {
  242. size = (size + 1) * sizeof(dummy_list[0]);
  243. tmp = kmalloc(size, GFP_KERNEL);
  244. if (tmp == NULL) {
  245. printk(KERN_ERR "ldt_get_host_info: couldn't allocate "
  246. "host ldt list\n");
  247. goto out_free;
  248. }
  249. host_ldt_entries = tmp;
  250. }
  251. for (i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++) {
  252. if (ldt[i].a != 0 || ldt[i].b != 0)
  253. host_ldt_entries[k++] = i;
  254. }
  255. host_ldt_entries[k] = -1;
  256. out_free:
  257. free_pages((unsigned long)ldt, order);
  258. }
  259. long init_new_ldt(struct mm_context *new_mm, struct mm_context *from_mm)
  260. {
  261. struct user_desc desc;
  262. short * num_p;
  263. int i;
  264. long page, err=0;
  265. void *addr = NULL;
  266. mutex_init(&new_mm->arch.ldt.lock);
  267. if (!from_mm) {
  268. memset(&desc, 0, sizeof(desc));
  269. /*
  270. * Now we try to retrieve info about the ldt, we
  271. * inherited from the host. All ldt-entries found
  272. * will be reset in the following loop
  273. */
  274. ldt_get_host_info();
  275. for (num_p=host_ldt_entries; *num_p != -1; num_p++) {
  276. desc.entry_number = *num_p;
  277. err = write_ldt_entry(&new_mm->id, 1, &desc,
  278. &addr, *(num_p + 1) == -1);
  279. if (err)
  280. break;
  281. }
  282. new_mm->arch.ldt.entry_count = 0;
  283. goto out;
  284. }
  285. /*
  286. * Our local LDT is used to supply the data for
  287. * modify_ldt(READLDT), if PTRACE_LDT isn't available,
  288. * i.e., we have to use the stub for modify_ldt, which
  289. * can't handle the big read buffer of up to 64kB.
  290. */
  291. mutex_lock(&from_mm->arch.ldt.lock);
  292. if (from_mm->arch.ldt.entry_count <= LDT_DIRECT_ENTRIES)
  293. memcpy(new_mm->arch.ldt.u.entries, from_mm->arch.ldt.u.entries,
  294. sizeof(new_mm->arch.ldt.u.entries));
  295. else {
  296. i = from_mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE;
  297. while (i-->0) {
  298. page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
  299. if (!page) {
  300. err = -ENOMEM;
  301. break;
  302. }
  303. new_mm->arch.ldt.u.pages[i] =
  304. (struct ldt_entry *) page;
  305. memcpy(new_mm->arch.ldt.u.pages[i],
  306. from_mm->arch.ldt.u.pages[i], PAGE_SIZE);
  307. }
  308. }
  309. new_mm->arch.ldt.entry_count = from_mm->arch.ldt.entry_count;
  310. mutex_unlock(&from_mm->arch.ldt.lock);
  311. out:
  312. return err;
  313. }
  314. void free_ldt(struct mm_context *mm)
  315. {
  316. int i;
  317. if (mm->arch.ldt.entry_count > LDT_DIRECT_ENTRIES) {
  318. i = mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE;
  319. while (i-- > 0)
  320. free_page((long) mm->arch.ldt.u.pages[i]);
  321. }
  322. mm->arch.ldt.entry_count = 0;
  323. }
  324. SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
  325. unsigned long , bytecount)
  326. {
  327. /* See non-um modify_ldt() for why we do this cast */
  328. return (unsigned int)do_modify_ldt_skas(func, ptr, bytecount);
  329. }