ldt.c 8.6 KB

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