windows.c 3.3 KB

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