ldt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * linux/kernel/ldt.c
  3. *
  4. * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
  5. * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/sched.h>
  9. #include <linux/string.h>
  10. #include <linux/mm.h>
  11. #include <linux/smp.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/slab.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/system.h>
  17. #include <asm/ldt.h>
  18. #include <asm/desc.h>
  19. #ifdef CONFIG_SMP /* avoids "defined but not used" warnig */
  20. static void flush_ldt(void *null)
  21. {
  22. if (current->active_mm)
  23. load_LDT(&current->active_mm->context);
  24. }
  25. #endif
  26. static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
  27. {
  28. void *oldldt;
  29. void *newldt;
  30. int oldsize;
  31. if (mincount <= pc->size)
  32. return 0;
  33. oldsize = pc->size;
  34. mincount = (mincount+511)&(~511);
  35. if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
  36. newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
  37. else
  38. newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL);
  39. if (!newldt)
  40. return -ENOMEM;
  41. if (oldsize)
  42. memcpy(newldt, pc->ldt, oldsize*LDT_ENTRY_SIZE);
  43. oldldt = pc->ldt;
  44. memset(newldt+oldsize*LDT_ENTRY_SIZE, 0, (mincount-oldsize)*LDT_ENTRY_SIZE);
  45. pc->ldt = newldt;
  46. wmb();
  47. pc->size = mincount;
  48. wmb();
  49. if (reload) {
  50. #ifdef CONFIG_SMP
  51. cpumask_t mask;
  52. preempt_disable();
  53. load_LDT(pc);
  54. mask = cpumask_of_cpu(smp_processor_id());
  55. if (!cpus_equal(current->mm->cpu_vm_mask, mask))
  56. smp_call_function(flush_ldt, NULL, 1, 1);
  57. preempt_enable();
  58. #else
  59. load_LDT(pc);
  60. #endif
  61. }
  62. if (oldsize) {
  63. if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
  64. vfree(oldldt);
  65. else
  66. kfree(oldldt);
  67. }
  68. return 0;
  69. }
  70. static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
  71. {
  72. int err = alloc_ldt(new, old->size, 0);
  73. if (err < 0)
  74. return err;
  75. memcpy(new->ldt, old->ldt, old->size*LDT_ENTRY_SIZE);
  76. return 0;
  77. }
  78. /*
  79. * we do not have to muck with descriptors here, that is
  80. * done in switch_mm() as needed.
  81. */
  82. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  83. {
  84. struct mm_struct * old_mm;
  85. int retval = 0;
  86. init_MUTEX(&mm->context.sem);
  87. mm->context.size = 0;
  88. old_mm = current->mm;
  89. if (old_mm && old_mm->context.size > 0) {
  90. down(&old_mm->context.sem);
  91. retval = copy_ldt(&mm->context, &old_mm->context);
  92. up(&old_mm->context.sem);
  93. }
  94. return retval;
  95. }
  96. /*
  97. * No need to lock the MM as we are the last user
  98. */
  99. void destroy_context(struct mm_struct *mm)
  100. {
  101. if (mm->context.size) {
  102. if (mm == current->active_mm)
  103. clear_LDT();
  104. if (mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
  105. vfree(mm->context.ldt);
  106. else
  107. kfree(mm->context.ldt);
  108. mm->context.size = 0;
  109. }
  110. }
  111. static int read_ldt(void __user * ptr, unsigned long bytecount)
  112. {
  113. int err;
  114. unsigned long size;
  115. struct mm_struct * mm = current->mm;
  116. if (!mm->context.size)
  117. return 0;
  118. if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
  119. bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
  120. down(&mm->context.sem);
  121. size = mm->context.size*LDT_ENTRY_SIZE;
  122. if (size > bytecount)
  123. size = bytecount;
  124. err = 0;
  125. if (copy_to_user(ptr, mm->context.ldt, size))
  126. err = -EFAULT;
  127. up(&mm->context.sem);
  128. if (err < 0)
  129. goto error_return;
  130. if (size != bytecount) {
  131. /* zero-fill the rest */
  132. if (clear_user(ptr+size, bytecount-size) != 0) {
  133. err = -EFAULT;
  134. goto error_return;
  135. }
  136. }
  137. return bytecount;
  138. error_return:
  139. return err;
  140. }
  141. static int read_default_ldt(void __user * ptr, unsigned long bytecount)
  142. {
  143. int err;
  144. unsigned long size;
  145. void *address;
  146. err = 0;
  147. address = &default_ldt[0];
  148. size = 5*sizeof(struct desc_struct);
  149. if (size > bytecount)
  150. size = bytecount;
  151. err = size;
  152. if (copy_to_user(ptr, address, size))
  153. err = -EFAULT;
  154. return err;
  155. }
  156. static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
  157. {
  158. struct mm_struct * mm = current->mm;
  159. __u32 entry_1, entry_2;
  160. int error;
  161. struct user_desc ldt_info;
  162. error = -EINVAL;
  163. if (bytecount != sizeof(ldt_info))
  164. goto out;
  165. error = -EFAULT;
  166. if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  167. goto out;
  168. error = -EINVAL;
  169. if (ldt_info.entry_number >= LDT_ENTRIES)
  170. goto out;
  171. if (ldt_info.contents == 3) {
  172. if (oldmode)
  173. goto out;
  174. if (ldt_info.seg_not_present == 0)
  175. goto out;
  176. }
  177. down(&mm->context.sem);
  178. if (ldt_info.entry_number >= mm->context.size) {
  179. error = alloc_ldt(&current->mm->context, ldt_info.entry_number+1, 1);
  180. if (error < 0)
  181. goto out_unlock;
  182. }
  183. /* Allow LDTs to be cleared by the user. */
  184. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  185. if (oldmode || LDT_empty(&ldt_info)) {
  186. entry_1 = 0;
  187. entry_2 = 0;
  188. goto install;
  189. }
  190. }
  191. entry_1 = LDT_entry_a(&ldt_info);
  192. entry_2 = LDT_entry_b(&ldt_info);
  193. if (oldmode)
  194. entry_2 &= ~(1 << 20);
  195. /* Install the new entry ... */
  196. install:
  197. write_ldt_entry(mm->context.ldt, ldt_info.entry_number, entry_1, entry_2);
  198. error = 0;
  199. out_unlock:
  200. up(&mm->context.sem);
  201. out:
  202. return error;
  203. }
  204. asmlinkage int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
  205. {
  206. int ret = -ENOSYS;
  207. switch (func) {
  208. case 0:
  209. ret = read_ldt(ptr, bytecount);
  210. break;
  211. case 1:
  212. ret = write_ldt(ptr, bytecount, 1);
  213. break;
  214. case 2:
  215. ret = read_default_ldt(ptr, bytecount);
  216. break;
  217. case 0x11:
  218. ret = write_ldt(ptr, bytecount, 0);
  219. break;
  220. }
  221. return ret;
  222. }