source: release/1.7/source/lisp-kernel/x86-asmutils32.s @ 15267

Last change on this file since 15267 was 14619, checked in by rme, 8 years ago

Merge shrink-tcr branch. This enables the 32-bit Windows lisp to run
on 64-bit Windows.

On 32-bit x86 ports, we expect to use a segment register to point to a
block of thread-local data called the TCR (thread context record).
This has always been kind of a bother on 32-bit Windows: we have been
using a kludge that allows us to use the %es segment register
(conditionalized on WIN32_ES_HACK).

Unfortunately, 64-bit Windows doesn't support using an LDT. This is
why the 32-bit lisp wouldn't run on 64-bit Windows.

The new scheme is to use some of the TlsSlots? (part of the Windows
TEB) for the most important parts of the TCR, and to introduce an "aux
vector" for the remaining TCR slots. Since %fs points to the TEB, we
can make this work. We reserve the last 34 (of 64) slots for our use,
and will die if we don't get them.

Microsoft's documentation says not to access the TlsSlots? directly
(you're supposed to use TlsGetValue/TlsSetValue?), so we're treading on
undocumented ground. Frankly, we've done worse.

This change introduces some ugliness. In lisp kernel C files, there's
a TCR_AUX(tcr) macro that expands to "tcr->aux" on win32, and to "tcr"
elsewhere.

If lisp or lap code has a pointer to a TCR, it's necessary to subtract
off target::tcr-bias (which on Windows/x86 is #xe10, the offset from
%fs to the TlsSlots? in the Windows TEB). We also sometimes have to load
target::tcr.aux to get at data which has been moved there.

These changes should only affect Windows/x86. The story on the other
platforms is just the same as before.

File size: 8.4 KB
Line 
1/*   Copyright (C) 2005-2009 Clozure Associates */
2/*   This file is part of Clozure CL.   */
3 
4/*   Clozure CL is licensed under the terms of the Lisp Lesser GNU Public */
5/*   License , known as the LLGPL and distributed with Clozure CL as the */
6/*   file "LICENSE".  The LLGPL consists of a preamble and the LGPL, */
7/*   which is distributed with Clozure CL as the file "LGPL".  Where these */
8/*   conflict, the preamble takes precedence.   */
9 
10/*   Clozure CL is referenced in the preamble as the "LIBRARY." */
11 
12/*   The LLGPL is also available online at */
13/*   http://opensource.franz.com/preamble.html */
14
15
16       
17
18        include(lisp.s)
19
20        _beginfile
21
22_exportfn(C(current_stack_pointer))
23        __(movl %esp,%eax)
24        __(ret)
25_endfn
26                       
27_exportfn(C(count_leading_zeros))
28        __(bsr 4(%esp),%eax)
29        __(xor $31,%eax)
30        __(ret)
31_endfn
32
33_exportfn(C(noop))
34        __(ret)
35_endfn
36
37_exportfn(C(set_mxcsr))
38        __(ldmxcsr 4(%esp))
39        __(ret)
40_endfn
41       
42_exportfn(C(get_mxcsr))
43        __(push $0)
44        __(stmxcsr (%esp))
45        __(pop %eax)
46        __(ret)
47_endfn
48
49_exportfn(C(save_fp_context))
50_endfn
51       
52_exportfn(C(restore_fp_context))
53_endfn                       
54
55/*  Atomically store new in *p, if *p == old. */
56/*  Return actual old value. */
57/* natural store_conditional(natural *p, natural old, natural new) */
58_exportfn(C(store_conditional))
59        __(movl 12(%esp),%edx)  /* new */
60        __(movl 8(%esp),%eax)   /* old */
61        __(movl 4(%esp),%ecx)   /* ptr */
62        __(lock)
63        __(cmpxchgl %edx,(%ecx))
64        __(cmovne %edx,%eax)
65        __(ret)
66_endfn
67
68/*      Atomically store val in *p; return previous *p */
69/*      of *%rdi. */
70/* signed_natural atomic_swap(signed_natural *p, signed_natural val) */
71_exportfn(C(atomic_swap))
72        __(movl 8(%esp),%eax)
73        __(movl 4(%esp),%edx)
74        __(lock)
75        __(xchg %eax,(%edx))
76        __(ret)
77_endfn
78
79/*      Logior the value in *p with mask (presumably a */
80/*      bitmask with exactly 1 bit set.)  Return non-zero if any of */
81/*      the bits in that bitmask were already set. */
82/* natural atomic_ior(natural *p, natural mask) */
83_exportfn(C(atomic_ior))
84        __(movl 4(%esp),%edx)   /* ptr */
850:      __(movl (%edx),%eax)
86        __(movl %eax,%ecx)
87        __(orl 8(%esp),%ecx)
88        __(lock)
89        __(cmpxchg %ecx,(%edx))
90        __(jnz 0b)
91        __(andl 8(%esp),%eax)
92        __(ret)
93_endfn
94       
95       
96/* Logand the value in *p with mask (presumably a bitmask with exactly 1 */
97/* bit set.)  Return the value now in *p (for some value of "now"). */
98/* natural atomic_and(natural *p, natural mask) */
99_exportfn(C(atomic_and))
100        __(movl 4(%esp),%edx)
1010:      __(movl (%edx),%eax)
102        __(movl %eax,%ecx)
103        __(and 8(%esp),%ecx)
104        __(lock)
105        __(cmpxchg %ecx,(%edx))
106        __(jnz 0b)
107        __(movl %ecx,%eax)
108        __(ret)
109_endfn
110
111
112        __ifdef(`DARWIN')
113_exportfn(C(pseudo_sigreturn))
114        __(hlt)
115        __(jmp C(pseudo_sigreturn))
116_endfn
117        __endif   
118
119/* int cpuid (int code, int *pebx, int *pecx, int *pedx)  */
120_exportfn(C(cpuid))
121        __(push %ebx)           /* %ebx is non-volatile */
122        __(push %esi)           /* ditto here */
123        __(movl 12(%esp),%eax)
124        __(xorl %ecx,%ecx)
125        __(cpuid)
126        __(movl 16(%esp),%esi)
127        __(movl %ebx,(%esi))
128        __(movl 20(%esp),%esi)
129        __(movl %ecx,(%esi))
130        __(movl 24(%esp),%esi)
131        __(movl %edx,(%esi))
132        __(pop %esi)
133        __(pop %ebx)
134        __(ret)
135_endfn
136
137/* switch_to_foreign_stack(new_sp, func, arg_0, arg_1, arg_2)  */
138/*   Not fully general, but should get us off of the signal stack */
139/* Beware: on Darwin, GDB can get very confused by this code, and
140   doesn't really get unconfused until the target function - the
141   handler - has built its stack frame
142   The lone caller of this function passes 3 arguments (besides
143   the new stack pointer and the handler address.)
144   On platforms where the C stack must be 16-byte aligned, pushing
145   a 4th word helps make the stack aligned before the return
146   address is (re-)pushed.
147   On Linux, there are severe constraints on what the top of stack
148   can look like when rt_sigreturn (the code at the return address)
149   runs, and there aren't any constraints on stack alignment, so
150   we don't push the extra word on the new stack.*/
151_exportfn(C(switch_to_foreign_stack))
152        __(addl $4,%esp)        /* discard return address, on wrong stack */
153        __(pop %edi)            /* new esp */
154        __(pop %esi)            /* handler */
155        __(pop %eax)            /* arg_0 */
156        __(pop %ebx)            /* arg_1 */
157        __(pop %ecx)            /* arg_2 */
158        __(mov %edi,%esp)
159        __(pop %edi)            /* Return address pushed by caller */
160        __ifndef(`LINUX')
161        __(push $0)             /* For alignment. See comment above */
162        __endif
163        __(push %ecx)           /* arg_2 */
164        __(push %ebx)           /* arg_1 */
165        __(push %eax)           /* arg_0 */
166        __(push %edi)           /* return address */
167        __(jmp *%esi)           /* On some platforms, we don't really return */
168_endfn
169
170        __ifdef(`FREEBSD')
171        .globl C(sigreturn)
172_exportfn(C(freebsd_sigreturn))
173        __(jmp C(sigreturn))
174_endfn
175        __endif
176
177        __ifdef(`DARWIN')
178_exportfn(C(darwin_sigreturn))
179/* Need to set the sigreturn 'infostyle' argument, which is mostly
180   undocumented.  On x8632 Darwin, sigtramp() sets it to 0x1e, and
181   since we're trying to do what sigtramp() would do if we'd returned
182   to it ... */
183        __(movl $0x1e,8(%esp))
184        __(movl $0xb8,%eax)     /* SYS_sigreturn */
185        __(int $0x80)
186        __(ret)                 /* shouldn't return */
187
188_endfn
189        __endif       
190               
191_exportfn(C(get_vector_registers))
192        __(ret)
193_endfn
194
195_exportfn(C(put_vector_registers))
196        __(ret)
197_endfn                         
198
199        __ifdef(`WIN_32')
200_exportfn(C(restore_windows_context))
201Xrestore_windows_context_start:
202        __(movl 4(%esp),%ecx)   /* context */
203        __(movl 12(%esp),%edx)  /* old valence */
204        __(movl 8(%esp),%eax)   /* tcr */
205        __(movl %edx,rcontext(tcr.valence))
206        __(movl $0,rcontext(tcr.pending_exception_context))
207        __(frstor win32_context.FloatSave(%ecx))
208        /* Windows doesn't bother to align the context, so use
209          'movupd' here */
210        __(movupd win32_context.Xmm0(%ecx),%xmm0)
211        __(movupd win32_context.Xmm1(%ecx),%xmm1)
212        __(movupd win32_context.Xmm2(%ecx),%xmm2)
213        __(movupd win32_context.Xmm3(%ecx),%xmm3)
214        __(movupd win32_context.Xmm4(%ecx),%xmm4)
215        __(movupd win32_context.Xmm5(%ecx),%xmm5)
216        __(movupd win32_context.Xmm6(%ecx),%xmm6)
217        __(movupd win32_context.Xmm7(%ecx),%xmm7)
218        __(ldmxcsr win32_context.MXCSR(%ecx))
219        __(movl win32_context.Ebp(%ecx),%ebp)
220        __(movl win32_context.Edi(%ecx),%edi)
221        __(movl win32_context.Esi(%ecx),%esi)
222        __(movl win32_context.Edx(%ecx),%edx)
223        __(movl win32_context.Ebx(%ecx),%ebx)
224        __(movl win32_context.Eax(%ecx),%eax)
225        __(movl win32_context.Esp(%ecx),%esp)
226        __(pushl win32_context.EFlags(%ecx))
227        __(pushl %cs)
228        __(pushl win32_context.Eip(%ecx))       
229        /* This must be the last thing before the iret, e.g., if we're
230        interrupted before the iret, the context we're returning to here
231        is still in %ecx.  If we're interrupted -at- the iret, then
232        everything but that which the iret will restore has been restored. */
233        __(movl win32_context.Ecx(%ecx),%ecx)
234Xrestore_windows_context_iret:           
235        __(iret)
236Xrestore_windows_context_end:             
237        __(nop)
238_endfn
239       
240_exportfn(C(windows_switch_to_foreign_stack))
241        __(pop %eax)
242        __(pop %ebx)            /* new %esp */
243        __(pop %ecx)            /* handler */
244        __(pop %edx)            /* arg */
245        __(movl %ebx,%esp)
246        __(subl $0x10,%esp)
247        __(movl %edx,(%esp))
248        __(push %eax)
249        __(jmp *%ecx)
250_endfn       
251
252        .data
253        .globl C(restore_windows_context_start)
254        .globl C(restore_windows_context_end)
255        .globl C(restore_windows_context_iret)
256C(restore_windows_context_start):  .long Xrestore_windows_context_start
257C(restore_windows_context_end): .long Xrestore_windows_context_end
258C(restore_windows_context_iret): .long Xrestore_windows_context_iret
259        .text
260       
261/* Something that we shouldn't return to */
262_exportfn(C(windows_halt))
263        __(hlt)
264_endfn         
265
266_exportfn(C(ensure_safe_for_string_operations))
267        __ifdef(`WIN32_ES_HACK')
268        __(movw %es,%ax)
269        __(movw %ds,%dx)
270        __(cmpw %ax,%dx)
271        __(jne 9f)
2720:      __(movw %dx,%es)
273        __endif
274        __(cld)       
275        __(ret)
276        __ifdef(`WIN32_ES_HACK')
2779:      __(hlt)
278        __(jmp 0b)
279        __endif
280_endfn                                       
281        __endif
282        _endfile
283
Note: See TracBrowser for help on using the repository browser.