ldt_32.c 5.0 KB

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