File: /usr/src/linux/arch/sparc/kernel/windows.c

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