fault.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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_ARCH_S390X
  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_ARCH_S390X */
  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_ARCH_S390X */
  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. extern 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. #ifndef CONFIG_ARCH_S390X
  318. typedef struct _pseudo_wait_t {
  319. struct _pseudo_wait_t *next;
  320. wait_queue_head_t queue;
  321. unsigned long address;
  322. int resolved;
  323. } pseudo_wait_t;
  324. static pseudo_wait_t *pseudo_lock_queue = NULL;
  325. static spinlock_t pseudo_wait_spinlock; /* spinlock to protect lock queue */
  326. /*
  327. * This routine handles 'pagex' pseudo page faults.
  328. */
  329. asmlinkage void
  330. do_pseudo_page_fault(struct pt_regs *regs, unsigned long error_code)
  331. {
  332. pseudo_wait_t wait_struct;
  333. pseudo_wait_t *ptr, *last, *next;
  334. unsigned long address;
  335. /*
  336. * get the failing address
  337. * more specific the segment and page table portion of
  338. * the address
  339. */
  340. address = S390_lowcore.trans_exc_code & 0xfffff000;
  341. if (address & 0x80000000) {
  342. /* high bit set -> a page has been swapped in by VM */
  343. address &= 0x7fffffff;
  344. spin_lock(&pseudo_wait_spinlock);
  345. last = NULL;
  346. ptr = pseudo_lock_queue;
  347. while (ptr != NULL) {
  348. next = ptr->next;
  349. if (address == ptr->address) {
  350. /*
  351. * This is one of the processes waiting
  352. * for the page. Unchain from the queue.
  353. * There can be more than one process
  354. * waiting for the same page. VM presents
  355. * an initial and a completion interrupt for
  356. * every process that tries to access a
  357. * page swapped out by VM.
  358. */
  359. if (last == NULL)
  360. pseudo_lock_queue = next;
  361. else
  362. last->next = next;
  363. /* now wake up the process */
  364. ptr->resolved = 1;
  365. wake_up(&ptr->queue);
  366. } else
  367. last = ptr;
  368. ptr = next;
  369. }
  370. spin_unlock(&pseudo_wait_spinlock);
  371. } else {
  372. /* Pseudo page faults in kernel mode is a bad idea */
  373. if (!(regs->psw.mask & PSW_MASK_PSTATE)) {
  374. /*
  375. * VM presents pseudo page faults if the interrupted
  376. * state was not disabled for interrupts. So we can
  377. * get pseudo page fault interrupts while running
  378. * in kernel mode. We simply access the page here
  379. * while we are running disabled. VM will then swap
  380. * in the page synchronously.
  381. */
  382. if (check_user_space(regs, error_code) == 0)
  383. /* dereference a virtual kernel address */
  384. __asm__ __volatile__ (
  385. " ic 0,0(%0)"
  386. : : "a" (address) : "0");
  387. else
  388. /* dereference a virtual user address */
  389. __asm__ __volatile__ (
  390. " la 2,0(%0)\n"
  391. " sacf 512\n"
  392. " ic 2,0(2)\n"
  393. "0:sacf 0\n"
  394. ".section __ex_table,\"a\"\n"
  395. " .align 4\n"
  396. " .long 0b,0b\n"
  397. ".previous"
  398. : : "a" (address) : "2" );
  399. return;
  400. }
  401. /* initialize and add element to pseudo_lock_queue */
  402. init_waitqueue_head (&wait_struct.queue);
  403. wait_struct.address = address;
  404. wait_struct.resolved = 0;
  405. spin_lock(&pseudo_wait_spinlock);
  406. wait_struct.next = pseudo_lock_queue;
  407. pseudo_lock_queue = &wait_struct;
  408. spin_unlock(&pseudo_wait_spinlock);
  409. /*
  410. * The instruction that caused the program check will
  411. * be repeated. Don't signal single step via SIGTRAP.
  412. */
  413. clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
  414. /* go to sleep */
  415. wait_event(wait_struct.queue, wait_struct.resolved);
  416. }
  417. }
  418. #endif /* CONFIG_ARCH_S390X */
  419. #ifdef CONFIG_PFAULT
  420. /*
  421. * 'pfault' pseudo page faults routines.
  422. */
  423. static int pfault_disable = 0;
  424. static int __init nopfault(char *str)
  425. {
  426. pfault_disable = 1;
  427. return 1;
  428. }
  429. __setup("nopfault", nopfault);
  430. typedef struct {
  431. __u16 refdiagc;
  432. __u16 reffcode;
  433. __u16 refdwlen;
  434. __u16 refversn;
  435. __u64 refgaddr;
  436. __u64 refselmk;
  437. __u64 refcmpmk;
  438. __u64 reserved;
  439. } __attribute__ ((packed)) pfault_refbk_t;
  440. int pfault_init(void)
  441. {
  442. pfault_refbk_t refbk =
  443. { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
  444. __PF_RES_FIELD };
  445. int rc;
  446. if (pfault_disable)
  447. return -1;
  448. __asm__ __volatile__(
  449. " diag %1,%0,0x258\n"
  450. "0: j 2f\n"
  451. "1: la %0,8\n"
  452. "2:\n"
  453. ".section __ex_table,\"a\"\n"
  454. " .align 4\n"
  455. #ifndef CONFIG_ARCH_S390X
  456. " .long 0b,1b\n"
  457. #else /* CONFIG_ARCH_S390X */
  458. " .quad 0b,1b\n"
  459. #endif /* CONFIG_ARCH_S390X */
  460. ".previous"
  461. : "=d" (rc) : "a" (&refbk) : "cc" );
  462. __ctl_set_bit(0, 9);
  463. return rc;
  464. }
  465. void pfault_fini(void)
  466. {
  467. pfault_refbk_t refbk =
  468. { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
  469. if (pfault_disable)
  470. return;
  471. __ctl_clear_bit(0,9);
  472. __asm__ __volatile__(
  473. " diag %0,0,0x258\n"
  474. "0:\n"
  475. ".section __ex_table,\"a\"\n"
  476. " .align 4\n"
  477. #ifndef CONFIG_ARCH_S390X
  478. " .long 0b,0b\n"
  479. #else /* CONFIG_ARCH_S390X */
  480. " .quad 0b,0b\n"
  481. #endif /* CONFIG_ARCH_S390X */
  482. ".previous"
  483. : : "a" (&refbk) : "cc" );
  484. }
  485. asmlinkage void
  486. pfault_interrupt(struct pt_regs *regs, __u16 error_code)
  487. {
  488. struct task_struct *tsk;
  489. __u16 subcode;
  490. /*
  491. * Get the external interruption subcode & pfault
  492. * initial/completion signal bit. VM stores this
  493. * in the 'cpu address' field associated with the
  494. * external interrupt.
  495. */
  496. subcode = S390_lowcore.cpu_addr;
  497. if ((subcode & 0xff00) != __SUBCODE_MASK)
  498. return;
  499. /*
  500. * Get the token (= address of the task structure of the affected task).
  501. */
  502. tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
  503. if (subcode & 0x0080) {
  504. /* signal bit is set -> a page has been swapped in by VM */
  505. if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
  506. /* Initial interrupt was faster than the completion
  507. * interrupt. pfault_wait is valid. Set pfault_wait
  508. * back to zero and wake up the process. This can
  509. * safely be done because the task is still sleeping
  510. * and can't produce new pfaults. */
  511. tsk->thread.pfault_wait = 0;
  512. wake_up_process(tsk);
  513. put_task_struct(tsk);
  514. }
  515. } else {
  516. /* signal bit not set -> a real page is missing. */
  517. get_task_struct(tsk);
  518. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  519. if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
  520. /* Completion interrupt was faster than the initial
  521. * interrupt (swapped in a -1 for pfault_wait). Set
  522. * pfault_wait back to zero and exit. This can be
  523. * done safely because tsk is running in kernel
  524. * mode and can't produce new pfaults. */
  525. tsk->thread.pfault_wait = 0;
  526. set_task_state(tsk, TASK_RUNNING);
  527. put_task_struct(tsk);
  528. } else
  529. set_tsk_need_resched(tsk);
  530. }
  531. }
  532. #endif