ldt.c 8.5 KB

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