ldt.c 8.5 KB

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