fault.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * arch/s390/mm/fault.c
  3. *
  4. * S390 version
  5. * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. * Author(s): Hartmut Penner (hp@de.ibm.com)
  7. * Ulrich Weigand (uweigand@de.ibm.com)
  8. *
  9. * Derived from "arch/i386/mm/fault.c"
  10. * Copyright (C) 1995 Linus Torvalds
  11. */
  12. #include <linux/config.h>
  13. #include <linux/signal.h>
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/string.h>
  18. #include <linux/types.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/mman.h>
  21. #include <linux/mm.h>
  22. #include <linux/smp.h>
  23. #include <linux/smp_lock.h>
  24. #include <linux/init.h>
  25. #include <linux/console.h>
  26. #include <linux/module.h>
  27. #include <linux/hardirq.h>
  28. #include <asm/system.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/pgtable.h>
  31. #ifndef CONFIG_64BIT
  32. #define __FAIL_ADDR_MASK 0x7ffff000
  33. #define __FIXUP_MASK 0x7fffffff
  34. #define __SUBCODE_MASK 0x0200
  35. #define __PF_RES_FIELD 0ULL
  36. #else /* CONFIG_64BIT */
  37. #define __FAIL_ADDR_MASK -4096L
  38. #define __FIXUP_MASK ~0L
  39. #define __SUBCODE_MASK 0x0600
  40. #define __PF_RES_FIELD 0x8000000000000000ULL
  41. #endif /* CONFIG_64BIT */
  42. #ifdef CONFIG_SYSCTL
  43. extern int sysctl_userprocess_debug;
  44. #endif
  45. extern void die(const char *,struct pt_regs *,long);
  46. extern spinlock_t timerlist_lock;
  47. /*
  48. * Unlock any spinlocks which will prevent us from getting the
  49. * message out (timerlist_lock is acquired through the
  50. * console unblank code)
  51. */
  52. void bust_spinlocks(int yes)
  53. {
  54. if (yes) {
  55. oops_in_progress = 1;
  56. } else {
  57. int loglevel_save = console_loglevel;
  58. console_unblank();
  59. oops_in_progress = 0;
  60. /*
  61. * OK, the message is on the console. Now we call printk()
  62. * without oops_in_progress set so that printk will give klogd
  63. * a poke. Hold onto your hats...
  64. */
  65. console_loglevel = 15;
  66. printk(" ");
  67. console_loglevel = loglevel_save;
  68. }
  69. }
  70. /*
  71. * Check which address space is addressed by the access
  72. * register in S390_lowcore.exc_access_id.
  73. * Returns 1 for user space and 0 for kernel space.
  74. */
  75. static int __check_access_register(struct pt_regs *regs, int error_code)
  76. {
  77. int areg = S390_lowcore.exc_access_id;
  78. if (areg == 0)
  79. /* Access via access register 0 -> kernel address */
  80. return 0;
  81. save_access_regs(current->thread.acrs);
  82. if (regs && areg < NUM_ACRS && current->thread.acrs[areg] <= 1)
  83. /*
  84. * access register contains 0 -> kernel address,
  85. * access register contains 1 -> user space address
  86. */
  87. return current->thread.acrs[areg];
  88. /* Something unhealthy was done with the access registers... */
  89. die("page fault via unknown access register", regs, error_code);
  90. do_exit(SIGKILL);
  91. return 0;
  92. }
  93. /*
  94. * Check which address space the address belongs to.
  95. * Returns 1 for user space and 0 for kernel space.
  96. */
  97. static inline int check_user_space(struct pt_regs *regs, int error_code)
  98. {
  99. /*
  100. * The lowest two bits of S390_lowcore.trans_exc_code indicate
  101. * which paging table was used:
  102. * 0: Primary Segment Table Descriptor
  103. * 1: STD determined via access register
  104. * 2: Secondary Segment Table Descriptor
  105. * 3: Home Segment Table Descriptor
  106. */
  107. int descriptor = S390_lowcore.trans_exc_code & 3;
  108. if (unlikely(descriptor == 1))
  109. return __check_access_register(regs, error_code);
  110. if (descriptor == 2)
  111. return current->thread.mm_segment.ar4;
  112. return descriptor != 0;
  113. }
  114. /*
  115. * Send SIGSEGV to task. This is an external routine
  116. * to keep the stack usage of do_page_fault small.
  117. */
  118. static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
  119. int si_code, unsigned long address)
  120. {
  121. struct siginfo si;
  122. #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
  123. #if defined(CONFIG_SYSCTL)
  124. if (sysctl_userprocess_debug)
  125. #endif
  126. {
  127. printk("User process fault: interruption code 0x%lX\n",
  128. error_code);
  129. printk("failing address: %lX\n", address);
  130. show_regs(regs);
  131. }
  132. #endif
  133. si.si_signo = SIGSEGV;
  134. si.si_code = si_code;
  135. si.si_addr = (void *) address;
  136. force_sig_info(SIGSEGV, &si, current);
  137. }
  138. /*
  139. * This routine handles page faults. It determines the address,
  140. * and the problem, and then passes it off to one of the appropriate
  141. * routines.
  142. *
  143. * error_code:
  144. * 04 Protection -> Write-Protection (suprression)
  145. * 10 Segment translation -> Not present (nullification)
  146. * 11 Page translation -> Not present (nullification)
  147. * 3b Region third trans. -> Not present (nullification)
  148. */
  149. static inline void
  150. do_exception(struct pt_regs *regs, unsigned long error_code, int is_protection)
  151. {
  152. struct task_struct *tsk;
  153. struct mm_struct *mm;
  154. struct vm_area_struct * vma;
  155. unsigned long address;
  156. int user_address;
  157. const struct exception_table_entry *fixup;
  158. int si_code = SEGV_MAPERR;
  159. tsk = current;
  160. mm = tsk->mm;
  161. /*
  162. * Check for low-address protection. This needs to be treated
  163. * as a special case because the translation exception code
  164. * field is not guaranteed to contain valid data in this case.
  165. */
  166. if (is_protection && !(S390_lowcore.trans_exc_code & 4)) {
  167. /* Low-address protection hit in kernel mode means
  168. NULL pointer write access in kernel mode. */
  169. if (!(regs->psw.mask & PSW_MASK_PSTATE)) {
  170. address = 0;
  171. user_address = 0;
  172. goto no_context;
  173. }
  174. /* Low-address protection hit in user mode 'cannot happen'. */
  175. die ("Low-address protection", regs, error_code);
  176. do_exit(SIGKILL);
  177. }
  178. /*
  179. * get the failing address
  180. * more specific the segment and page table portion of
  181. * the address
  182. */
  183. address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
  184. user_address = check_user_space(regs, error_code);
  185. /*
  186. * Verify that the fault happened in user space, that
  187. * we are not in an interrupt and that there is a
  188. * user context.
  189. */
  190. if (user_address == 0 || in_atomic() || !mm)
  191. goto no_context;
  192. /*
  193. * When we get here, the fault happened in the current
  194. * task's user address space, so we can switch on the
  195. * interrupts again and then search the VMAs
  196. */
  197. local_irq_enable();
  198. down_read(&mm->mmap_sem);
  199. vma = find_vma(mm, address);
  200. if (!vma)
  201. goto bad_area;
  202. if (vma->vm_start <= address)
  203. goto good_area;
  204. if (!(vma->vm_flags & VM_GROWSDOWN))
  205. goto bad_area;
  206. if (expand_stack(vma, address))
  207. goto bad_area;
  208. /*
  209. * Ok, we have a good vm_area for this memory access, so
  210. * we can handle it..
  211. */
  212. good_area:
  213. si_code = SEGV_ACCERR;
  214. if (!is_protection) {
  215. /* page not present, check vm flags */
  216. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  217. goto bad_area;
  218. } else {
  219. if (!(vma->vm_flags & VM_WRITE))
  220. goto bad_area;
  221. }
  222. survive:
  223. /*
  224. * If for any reason at all we couldn't handle the fault,
  225. * make sure we exit gracefully rather than endlessly redo
  226. * the fault.
  227. */
  228. switch (handle_mm_fault(mm, vma, address, is_protection)) {
  229. case VM_FAULT_MINOR:
  230. tsk->min_flt++;
  231. break;
  232. case VM_FAULT_MAJOR:
  233. tsk->maj_flt++;
  234. break;
  235. case VM_FAULT_SIGBUS:
  236. goto do_sigbus;
  237. case VM_FAULT_OOM:
  238. goto out_of_memory;
  239. default:
  240. BUG();
  241. }
  242. up_read(&mm->mmap_sem);
  243. /*
  244. * The instruction that caused the program check will
  245. * be repeated. Don't signal single step via SIGTRAP.
  246. */
  247. clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
  248. return;
  249. /*
  250. * Something tried to access memory that isn't in our memory map..
  251. * Fix it, but check if it's kernel or user first..
  252. */
  253. bad_area:
  254. up_read(&mm->mmap_sem);
  255. /* User mode accesses just cause a SIGSEGV */
  256. if (regs->psw.mask & PSW_MASK_PSTATE) {
  257. tsk->thread.prot_addr = address;
  258. tsk->thread.trap_no = error_code;
  259. do_sigsegv(regs, error_code, si_code, address);
  260. return;
  261. }
  262. no_context:
  263. /* Are we prepared to handle this kernel fault? */
  264. fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
  265. if (fixup) {
  266. regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
  267. return;
  268. }
  269. /*
  270. * Oops. The kernel tried to access some bad page. We'll have to
  271. * terminate things with extreme prejudice.
  272. */
  273. if (user_address == 0)
  274. printk(KERN_ALERT "Unable to handle kernel pointer dereference"
  275. " at virtual kernel address %p\n", (void *)address);
  276. else
  277. printk(KERN_ALERT "Unable to handle kernel paging request"
  278. " at virtual user address %p\n", (void *)address);
  279. die("Oops", regs, error_code);
  280. do_exit(SIGKILL);
  281. /*
  282. * We ran out of memory, or some other thing happened to us that made
  283. * us unable to handle the page fault gracefully.
  284. */
  285. out_of_memory:
  286. up_read(&mm->mmap_sem);
  287. if (tsk->pid == 1) {
  288. yield();
  289. goto survive;
  290. }
  291. printk("VM: killing process %s\n", tsk->comm);
  292. if (regs->psw.mask & PSW_MASK_PSTATE)
  293. do_exit(SIGKILL);
  294. goto no_context;
  295. do_sigbus:
  296. up_read(&mm->mmap_sem);
  297. /*
  298. * Send a sigbus, regardless of whether we were in kernel
  299. * or user mode.
  300. */
  301. tsk->thread.prot_addr = address;
  302. tsk->thread.trap_no = error_code;
  303. force_sig(SIGBUS, tsk);
  304. /* Kernel mode? Handle exceptions or die */
  305. if (!(regs->psw.mask & PSW_MASK_PSTATE))
  306. goto no_context;
  307. }
  308. void do_protection_exception(struct pt_regs *regs, unsigned long error_code)
  309. {
  310. regs->psw.addr -= (error_code >> 16);
  311. do_exception(regs, 4, 1);
  312. }
  313. void do_dat_exception(struct pt_regs *regs, unsigned long error_code)
  314. {
  315. do_exception(regs, error_code & 0xff, 0);
  316. }
  317. #ifdef CONFIG_PFAULT
  318. /*
  319. * 'pfault' pseudo page faults routines.
  320. */
  321. static int pfault_disable = 0;
  322. static int __init nopfault(char *str)
  323. {
  324. pfault_disable = 1;
  325. return 1;
  326. }
  327. __setup("nopfault", nopfault);
  328. typedef struct {
  329. __u16 refdiagc;
  330. __u16 reffcode;
  331. __u16 refdwlen;
  332. __u16 refversn;
  333. __u64 refgaddr;
  334. __u64 refselmk;
  335. __u64 refcmpmk;
  336. __u64 reserved;
  337. } __attribute__ ((packed)) pfault_refbk_t;
  338. int pfault_init(void)
  339. {
  340. pfault_refbk_t refbk =
  341. { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
  342. __PF_RES_FIELD };
  343. int rc;
  344. if (pfault_disable)
  345. return -1;
  346. __asm__ __volatile__(
  347. " diag %1,%0,0x258\n"
  348. "0: j 2f\n"
  349. "1: la %0,8\n"
  350. "2:\n"
  351. ".section __ex_table,\"a\"\n"
  352. " .align 4\n"
  353. #ifndef CONFIG_64BIT
  354. " .long 0b,1b\n"
  355. #else /* CONFIG_64BIT */
  356. " .quad 0b,1b\n"
  357. #endif /* CONFIG_64BIT */
  358. ".previous"
  359. : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc" );
  360. __ctl_set_bit(0, 9);
  361. return rc;
  362. }
  363. void pfault_fini(void)
  364. {
  365. pfault_refbk_t refbk =
  366. { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
  367. if (pfault_disable)
  368. return;
  369. __ctl_clear_bit(0,9);
  370. __asm__ __volatile__(
  371. " diag %0,0,0x258\n"
  372. "0:\n"
  373. ".section __ex_table,\"a\"\n"
  374. " .align 4\n"
  375. #ifndef CONFIG_64BIT
  376. " .long 0b,0b\n"
  377. #else /* CONFIG_64BIT */
  378. " .quad 0b,0b\n"
  379. #endif /* CONFIG_64BIT */
  380. ".previous"
  381. : : "a" (&refbk), "m" (refbk) : "cc" );
  382. }
  383. asmlinkage void
  384. pfault_interrupt(struct pt_regs *regs, __u16 error_code)
  385. {
  386. struct task_struct *tsk;
  387. __u16 subcode;
  388. /*
  389. * Get the external interruption subcode & pfault
  390. * initial/completion signal bit. VM stores this
  391. * in the 'cpu address' field associated with the
  392. * external interrupt.
  393. */
  394. subcode = S390_lowcore.cpu_addr;
  395. if ((subcode & 0xff00) != __SUBCODE_MASK)
  396. return;
  397. /*
  398. * Get the token (= address of the task structure of the affected task).
  399. */
  400. tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
  401. if (subcode & 0x0080) {
  402. /* signal bit is set -> a page has been swapped in by VM */
  403. if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
  404. /* Initial interrupt was faster than the completion
  405. * interrupt. pfault_wait is valid. Set pfault_wait
  406. * back to zero and wake up the process. This can
  407. * safely be done because the task is still sleeping
  408. * and can't produce new pfaults. */
  409. tsk->thread.pfault_wait = 0;
  410. wake_up_process(tsk);
  411. put_task_struct(tsk);
  412. }
  413. } else {
  414. /* signal bit not set -> a real page is missing. */
  415. get_task_struct(tsk);
  416. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  417. if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
  418. /* Completion interrupt was faster than the initial
  419. * interrupt (swapped in a -1 for pfault_wait). Set
  420. * pfault_wait back to zero and exit. This can be
  421. * done safely because tsk is running in kernel
  422. * mode and can't produce new pfaults. */
  423. tsk->thread.pfault_wait = 0;
  424. set_task_state(tsk, TASK_RUNNING);
  425. put_task_struct(tsk);
  426. } else
  427. set_tsk_need_resched(tsk);
  428. }
  429. }
  430. #endif