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