windows.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* windows.c: Routines to deal with register window management
  2. * at the C-code level.
  3. *
  4. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/string.h>
  9. #include <linux/mm.h>
  10. #include <linux/smp.h>
  11. #include <linux/smp_lock.h>
  12. #include <asm/uaccess.h>
  13. /* Do save's until all user register windows are out of the cpu. */
  14. void flush_user_windows(void)
  15. {
  16. register int ctr asm("g5");
  17. ctr = 0;
  18. __asm__ __volatile__(
  19. "\n1:\n\t"
  20. "ld [%%g6 + %2], %%g4\n\t"
  21. "orcc %%g0, %%g4, %%g0\n\t"
  22. "add %0, 1, %0\n\t"
  23. "bne 1b\n\t"
  24. " save %%sp, -64, %%sp\n"
  25. "2:\n\t"
  26. "subcc %0, 1, %0\n\t"
  27. "bne 2b\n\t"
  28. " restore %%g0, %%g0, %%g0\n"
  29. : "=&r" (ctr)
  30. : "0" (ctr),
  31. "i" ((const unsigned long)TI_UWINMASK)
  32. : "g4", "cc");
  33. }
  34. static inline void shift_window_buffer(int first_win, int last_win, struct thread_info *tp)
  35. {
  36. int i;
  37. for(i = first_win; i < last_win; i++) {
  38. tp->rwbuf_stkptrs[i] = tp->rwbuf_stkptrs[i+1];
  39. memcpy(&tp->reg_window[i], &tp->reg_window[i+1], sizeof(struct reg_window32));
  40. }
  41. }
  42. /* Place as many of the user's current register windows
  43. * on the stack that we can. Even if the %sp is unaligned
  44. * we still copy the window there, the only case that we don't
  45. * succeed is if the %sp points to a bum mapping altogether.
  46. * setup_frame() and do_sigreturn() use this before shifting
  47. * the user stack around. Future instruction and hardware
  48. * bug workaround routines will need this functionality as
  49. * well.
  50. */
  51. void synchronize_user_stack(void)
  52. {
  53. struct thread_info *tp = current_thread_info();
  54. int window;
  55. flush_user_windows();
  56. if(!tp->w_saved)
  57. return;
  58. /* Ok, there is some dirty work to do. */
  59. for(window = tp->w_saved - 1; window >= 0; window--) {
  60. unsigned long sp = tp->rwbuf_stkptrs[window];
  61. /* Ok, let it rip. */
  62. if (copy_to_user((char __user *) sp, &tp->reg_window[window],
  63. sizeof(struct reg_window32)))
  64. continue;
  65. shift_window_buffer(window, tp->w_saved - 1, tp);
  66. tp->w_saved--;
  67. }
  68. }
  69. #if 0
  70. /* An optimization. */
  71. static inline void copy_aligned_window(void *dest, const void *src)
  72. {
  73. __asm__ __volatile__("ldd [%1], %%g2\n\t"
  74. "ldd [%1 + 0x8], %%g4\n\t"
  75. "std %%g2, [%0]\n\t"
  76. "std %%g4, [%0 + 0x8]\n\t"
  77. "ldd [%1 + 0x10], %%g2\n\t"
  78. "ldd [%1 + 0x18], %%g4\n\t"
  79. "std %%g2, [%0 + 0x10]\n\t"
  80. "std %%g4, [%0 + 0x18]\n\t"
  81. "ldd [%1 + 0x20], %%g2\n\t"
  82. "ldd [%1 + 0x28], %%g4\n\t"
  83. "std %%g2, [%0 + 0x20]\n\t"
  84. "std %%g4, [%0 + 0x28]\n\t"
  85. "ldd [%1 + 0x30], %%g2\n\t"
  86. "ldd [%1 + 0x38], %%g4\n\t"
  87. "std %%g2, [%0 + 0x30]\n\t"
  88. "std %%g4, [%0 + 0x38]\n\t" : :
  89. "r" (dest), "r" (src) :
  90. "g2", "g3", "g4", "g5");
  91. }
  92. #endif
  93. /* Try to push the windows in a threads window buffer to the
  94. * user stack. Unaligned %sp's are not allowed here.
  95. */
  96. void try_to_clear_window_buffer(struct pt_regs *regs, int who)
  97. {
  98. struct thread_info *tp = current_thread_info();
  99. int window;
  100. lock_kernel();
  101. flush_user_windows();
  102. for(window = 0; window < tp->w_saved; window++) {
  103. unsigned long sp = tp->rwbuf_stkptrs[window];
  104. if ((sp & 7) ||
  105. copy_to_user((char __user *) sp, &tp->reg_window[window],
  106. sizeof(struct reg_window32)))
  107. do_exit(SIGILL);
  108. }
  109. tp->w_saved = 0;
  110. unlock_kernel();
  111. }