2008-06-30 12:50:36 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-06-17 16:12:16 -06:00
|
|
|
#include "amd64/asm.h"
|
2008-06-30 12:50:36 -06:00
|
|
|
|
|
|
|
TEXT _rt0_amd64(SB),7,$-8
|
|
|
|
// copy arguments forward on an even stack
|
2009-11-17 09:20:58 -07:00
|
|
|
MOVQ 0(DI), AX // argc
|
|
|
|
LEAQ 8(DI), BX // argv
|
2008-06-30 12:50:36 -06:00
|
|
|
SUBQ $(4*8+7), SP // 2args 2auto
|
|
|
|
ANDQ $~7, SP
|
|
|
|
MOVQ AX, 16(SP)
|
|
|
|
MOVQ BX, 24(SP)
|
|
|
|
|
2008-08-04 17:43:49 -06:00
|
|
|
// set the per-goroutine and per-mach registers
|
2009-06-17 16:12:16 -06:00
|
|
|
LEAQ m0(SB), m
|
|
|
|
LEAQ g0(SB), g
|
|
|
|
MOVQ g, m_g0(m) // m has pointer to its g0
|
2008-06-30 12:50:36 -06:00
|
|
|
|
2008-07-11 20:16:39 -06:00
|
|
|
// create istack out of the given (operating system) stack
|
2008-12-19 04:13:39 -07:00
|
|
|
LEAQ (-8192+104)(SP), AX
|
2009-06-17 16:12:16 -06:00
|
|
|
MOVQ AX, g_stackguard(g)
|
|
|
|
MOVQ SP, g_stackbase(g)
|
2008-06-30 12:50:36 -06:00
|
|
|
|
2008-12-15 16:07:35 -07:00
|
|
|
CLD // convention is D is always left cleared
|
2008-06-30 12:50:36 -06:00
|
|
|
CALL check(SB)
|
|
|
|
|
|
|
|
MOVL 16(SP), AX // copy argc
|
|
|
|
MOVL AX, 0(SP)
|
|
|
|
MOVQ 24(SP), AX // copy argv
|
|
|
|
MOVQ AX, 8(SP)
|
|
|
|
CALL args(SB)
|
2008-09-09 12:50:14 -06:00
|
|
|
CALL osinit(SB)
|
2008-08-04 17:43:49 -06:00
|
|
|
CALL schedinit(SB)
|
2008-08-05 15:21:42 -06:00
|
|
|
|
2008-07-11 20:16:39 -06:00
|
|
|
// create a new goroutine to start program
|
2008-09-18 16:56:46 -06:00
|
|
|
PUSHQ $mainstart(SB) // entry
|
2009-06-17 16:12:16 -06:00
|
|
|
PUSHQ $0 // arg size
|
2010-01-25 19:52:55 -07:00
|
|
|
CALL ·newproc(SB)
|
2008-07-11 20:16:39 -06:00
|
|
|
POPQ AX
|
|
|
|
POPQ AX
|
2008-12-04 09:30:54 -07:00
|
|
|
|
2008-09-22 14:47:59 -06:00
|
|
|
// start this M
|
|
|
|
CALL mstart(SB)
|
2008-06-30 12:50:36 -06:00
|
|
|
|
2008-07-11 20:16:39 -06:00
|
|
|
CALL notok(SB) // never returns
|
2008-06-30 12:50:36 -06:00
|
|
|
RET
|
|
|
|
|
2008-09-18 16:56:46 -06:00
|
|
|
TEXT mainstart(SB),7,$0
|
2009-01-20 15:40:00 -07:00
|
|
|
CALL main·init(SB)
|
2008-09-18 16:56:46 -06:00
|
|
|
CALL initdone(SB)
|
|
|
|
CALL main·main(SB)
|
2008-09-22 14:47:59 -06:00
|
|
|
PUSHQ $0
|
2009-05-08 16:21:41 -06:00
|
|
|
CALL exit(SB)
|
2008-09-22 14:47:59 -06:00
|
|
|
POPQ AX
|
|
|
|
CALL notok(SB)
|
2008-09-18 16:56:46 -06:00
|
|
|
RET
|
|
|
|
|
2009-05-08 16:21:41 -06:00
|
|
|
TEXT breakpoint(SB),7,$0
|
2008-07-11 20:16:39 -06:00
|
|
|
BYTE $0xcc
|
2008-06-30 12:50:36 -06:00
|
|
|
RET
|
|
|
|
|
2008-07-11 20:16:39 -06:00
|
|
|
/*
|
|
|
|
* go-routine
|
|
|
|
*/
|
2008-07-07 18:59:32 -06:00
|
|
|
|
2009-06-17 16:12:16 -06:00
|
|
|
// uintptr gosave(Gobuf*)
|
|
|
|
// save state in Gobuf; setjmp
|
2008-07-11 20:16:39 -06:00
|
|
|
TEXT gosave(SB), 7, $0
|
|
|
|
MOVQ 8(SP), AX // gobuf
|
2009-06-17 16:12:16 -06:00
|
|
|
LEAQ 8(SP), BX // caller's SP
|
|
|
|
MOVQ BX, gobuf_sp(AX)
|
|
|
|
MOVQ 0(SP), BX // caller's PC
|
|
|
|
MOVQ BX, gobuf_pc(AX)
|
|
|
|
MOVQ g, gobuf_g(AX)
|
2008-07-11 20:16:39 -06:00
|
|
|
MOVL $0, AX // return 0
|
2008-06-30 12:50:36 -06:00
|
|
|
RET
|
|
|
|
|
2009-06-17 16:12:16 -06:00
|
|
|
// void gogo(Gobuf*, uintptr)
|
|
|
|
// restore state from Gobuf; longjmp
|
|
|
|
TEXT gogo(SB), 7, $0
|
|
|
|
MOVQ 16(SP), AX // return 2nd arg
|
|
|
|
MOVQ 8(SP), BX // gobuf
|
|
|
|
MOVQ gobuf_g(BX), g
|
|
|
|
MOVQ 0(g), CX // make sure g != nil
|
|
|
|
MOVQ gobuf_sp(BX), SP // restore SP
|
|
|
|
MOVQ gobuf_pc(BX), BX
|
|
|
|
JMP BX
|
|
|
|
|
|
|
|
// void gogocall(Gobuf*, void (*fn)(void))
|
|
|
|
// restore state from Gobuf but then call fn.
|
|
|
|
// (call fn, returning to state in Gobuf)
|
|
|
|
TEXT gogocall(SB), 7, $0
|
|
|
|
MOVQ 16(SP), AX // fn
|
|
|
|
MOVQ 8(SP), BX // gobuf
|
|
|
|
MOVQ gobuf_g(BX), g
|
|
|
|
MOVQ 0(g), CX // make sure g != nil
|
|
|
|
MOVQ gobuf_sp(BX), SP // restore SP
|
|
|
|
MOVQ gobuf_pc(BX), BX
|
|
|
|
PUSHQ BX
|
|
|
|
JMP AX
|
|
|
|
POPQ BX // not reached
|
|
|
|
|
2008-07-12 12:30:53 -06:00
|
|
|
/*
|
|
|
|
* support for morestack
|
|
|
|
*/
|
|
|
|
|
2009-06-17 16:12:16 -06:00
|
|
|
// Called during function prolog when more stack is needed.
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·morestack(SB),7,$0
|
2009-06-17 16:12:16 -06:00
|
|
|
// Called from f.
|
|
|
|
// Set m->morebuf to f's caller.
|
|
|
|
MOVQ 8(SP), AX // f's caller's PC
|
|
|
|
MOVQ AX, (m_morebuf+gobuf_pc)(m)
|
|
|
|
LEAQ 16(SP), AX // f's caller's SP
|
|
|
|
MOVQ AX, (m_morebuf+gobuf_sp)(m)
|
2009-07-08 19:16:09 -06:00
|
|
|
MOVQ AX, (m_morefp)(m)
|
2009-06-17 16:12:16 -06:00
|
|
|
MOVQ g, (m_morebuf+gobuf_g)(m)
|
|
|
|
|
|
|
|
// Set m->morepc to f's PC.
|
|
|
|
MOVQ 0(SP), AX
|
|
|
|
MOVQ AX, m_morepc(m)
|
|
|
|
|
|
|
|
// Call newstack on m's scheduling stack.
|
|
|
|
MOVQ m_g0(m), g
|
|
|
|
MOVQ (m_sched+gobuf_sp)(m), SP
|
|
|
|
CALL newstack(SB)
|
|
|
|
MOVQ $0, 0x1003 // crash if newstack returns
|
2009-07-08 19:16:09 -06:00
|
|
|
RET
|
|
|
|
|
|
|
|
// Called from reflection library. Mimics morestack,
|
|
|
|
// reuses stack growth code to create a frame
|
|
|
|
// with the desired args running the desired function.
|
|
|
|
//
|
|
|
|
// func call(fn *byte, arg *byte, argsize uint32).
|
|
|
|
TEXT reflect·call(SB), 7, $0
|
|
|
|
// Save our caller's state as the PC and SP to
|
|
|
|
// restore when returning from f.
|
|
|
|
MOVQ 0(SP), AX // our caller's PC
|
|
|
|
MOVQ AX, (m_morebuf+gobuf_pc)(m)
|
|
|
|
LEAQ 8(SP), AX // our caller's SP
|
|
|
|
MOVQ AX, (m_morebuf+gobuf_sp)(m)
|
|
|
|
MOVQ g, (m_morebuf+gobuf_g)(m)
|
|
|
|
|
|
|
|
// Set up morestack arguments to call f on a new stack.
|
2010-03-29 22:48:22 -06:00
|
|
|
// We set f's frame size to 1, as a hint to newstack
|
|
|
|
// that this is a call from reflect·call.
|
|
|
|
// If it turns out that f needs a larger frame than
|
|
|
|
// the default stack, f's usual stack growth prolog will
|
|
|
|
// allocate a new segment (and recopy the arguments).
|
2009-07-08 19:16:09 -06:00
|
|
|
MOVQ 8(SP), AX // fn
|
|
|
|
MOVQ 16(SP), BX // arg frame
|
|
|
|
MOVL 24(SP), CX // arg size
|
|
|
|
|
|
|
|
MOVQ AX, m_morepc(m) // f's PC
|
|
|
|
MOVQ BX, m_morefp(m) // argument frame pointer
|
|
|
|
MOVL CX, m_moreargs(m) // f's argument size
|
2010-03-29 22:48:22 -06:00
|
|
|
MOVL $1, m_moreframe(m) // f's frame size
|
2009-07-08 19:16:09 -06:00
|
|
|
|
|
|
|
// Call newstack on m's scheduling stack.
|
|
|
|
MOVQ m_g0(m), g
|
|
|
|
MOVQ (m_sched+gobuf_sp)(m), SP
|
|
|
|
CALL newstack(SB)
|
|
|
|
MOVQ $0, 0x1103 // crash if newstack returns
|
2009-06-17 16:12:16 -06:00
|
|
|
RET
|
|
|
|
|
|
|
|
// Return point when leaving stack.
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·lessstack(SB), 7, $0
|
2009-06-17 16:12:16 -06:00
|
|
|
// Save return value in m->cret
|
|
|
|
MOVQ AX, m_cret(m)
|
|
|
|
|
|
|
|
// Call oldstack on m's scheduling stack.
|
|
|
|
MOVQ m_g0(m), g
|
|
|
|
MOVQ (m_sched+gobuf_sp)(m), SP
|
|
|
|
CALL oldstack(SB)
|
|
|
|
MOVQ $0, 0x1004 // crash if oldstack returns
|
|
|
|
RET
|
|
|
|
|
2009-05-01 19:07:33 -06:00
|
|
|
// morestack trampolines
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·morestack00+0(SB),7,$0
|
2009-05-01 19:07:33 -06:00
|
|
|
MOVQ $0, AX
|
2009-06-17 17:31:02 -06:00
|
|
|
MOVQ AX, m_moreframe(m)
|
2010-01-25 19:52:55 -07:00
|
|
|
MOVQ $·morestack+0(SB), AX
|
2009-05-01 19:07:33 -06:00
|
|
|
JMP AX
|
|
|
|
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·morestack01+0(SB),7,$0
|
2009-05-01 19:07:33 -06:00
|
|
|
SHLQ $32, AX
|
2009-06-17 17:31:02 -06:00
|
|
|
MOVQ AX, m_moreframe(m)
|
2010-01-25 19:52:55 -07:00
|
|
|
MOVQ $·morestack+0(SB), AX
|
2009-05-01 19:07:33 -06:00
|
|
|
JMP AX
|
|
|
|
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·morestack10+0(SB),7,$0
|
2009-05-01 19:07:33 -06:00
|
|
|
MOVLQZX AX, AX
|
2009-06-17 17:31:02 -06:00
|
|
|
MOVQ AX, m_moreframe(m)
|
2010-01-25 19:52:55 -07:00
|
|
|
MOVQ $·morestack+0(SB), AX
|
2009-05-01 19:07:33 -06:00
|
|
|
JMP AX
|
|
|
|
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·morestack11+0(SB),7,$0
|
2009-06-17 17:31:02 -06:00
|
|
|
MOVQ AX, m_moreframe(m)
|
2010-01-25 19:52:55 -07:00
|
|
|
MOVQ $·morestack+0(SB), AX
|
2009-05-01 19:07:33 -06:00
|
|
|
JMP AX
|
|
|
|
|
2009-05-03 20:09:14 -06:00
|
|
|
// subcases of morestack01
|
|
|
|
// with const of 8,16,...48
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·morestack8(SB),7,$0
|
2009-05-03 20:09:14 -06:00
|
|
|
PUSHQ $1
|
2010-01-25 19:52:55 -07:00
|
|
|
MOVQ $·morestackx(SB), AX
|
2009-05-03 20:09:14 -06:00
|
|
|
JMP AX
|
|
|
|
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·morestack16(SB),7,$0
|
2009-05-03 20:09:14 -06:00
|
|
|
PUSHQ $2
|
2010-01-25 19:52:55 -07:00
|
|
|
MOVQ $·morestackx(SB), AX
|
2009-05-03 20:09:14 -06:00
|
|
|
JMP AX
|
|
|
|
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·morestack24(SB),7,$0
|
2009-05-03 20:09:14 -06:00
|
|
|
PUSHQ $3
|
2010-01-25 19:52:55 -07:00
|
|
|
MOVQ $·morestackx(SB), AX
|
2009-05-03 20:09:14 -06:00
|
|
|
JMP AX
|
|
|
|
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·morestack32(SB),7,$0
|
2009-05-03 20:09:14 -06:00
|
|
|
PUSHQ $4
|
2010-01-25 19:52:55 -07:00
|
|
|
MOVQ $·morestackx(SB), AX
|
2009-05-03 20:09:14 -06:00
|
|
|
JMP AX
|
|
|
|
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·morestack40(SB),7,$0
|
2009-05-03 20:09:14 -06:00
|
|
|
PUSHQ $5
|
2010-01-25 19:52:55 -07:00
|
|
|
MOVQ $·morestackx(SB), AX
|
2009-05-03 20:09:14 -06:00
|
|
|
JMP AX
|
|
|
|
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·morestack48(SB),7,$0
|
2009-05-03 20:09:14 -06:00
|
|
|
PUSHQ $6
|
2010-01-25 19:52:55 -07:00
|
|
|
MOVQ $·morestackx(SB), AX
|
2009-05-03 20:09:14 -06:00
|
|
|
JMP AX
|
|
|
|
|
2010-01-25 19:52:55 -07:00
|
|
|
TEXT ·morestackx(SB),7,$0
|
2009-06-17 16:12:16 -06:00
|
|
|
POPQ AX
|
|
|
|
SHLQ $35, AX
|
2009-06-17 17:31:02 -06:00
|
|
|
MOVQ AX, m_moreframe(m)
|
2010-01-25 19:52:55 -07:00
|
|
|
MOVQ $·morestack(SB), AX
|
2008-07-12 12:30:53 -06:00
|
|
|
JMP AX
|
|
|
|
|
2008-08-04 17:43:49 -06:00
|
|
|
// bool cas(int32 *val, int32 old, int32 new)
|
|
|
|
// Atomically:
|
|
|
|
// if(*val == old){
|
|
|
|
// *val = new;
|
|
|
|
// return 1;
|
2009-01-27 13:03:53 -07:00
|
|
|
// } else
|
2008-08-04 17:43:49 -06:00
|
|
|
// return 0;
|
|
|
|
TEXT cas(SB), 7, $0
|
|
|
|
MOVQ 8(SP), BX
|
|
|
|
MOVL 16(SP), AX
|
|
|
|
MOVL 20(SP), CX
|
|
|
|
LOCK
|
|
|
|
CMPXCHGL CX, 0(BX)
|
|
|
|
JZ 3(PC)
|
|
|
|
MOVL $0, AX
|
|
|
|
RET
|
|
|
|
MOVL $1, AX
|
|
|
|
RET
|
2009-01-27 13:03:53 -07:00
|
|
|
|
2009-06-03 00:02:12 -06:00
|
|
|
// void jmpdefer(fn, sp);
|
|
|
|
// called from deferreturn.
|
2009-01-27 13:03:53 -07:00
|
|
|
// 1. pop the caller
|
|
|
|
// 2. sub 5 bytes from the callers return
|
|
|
|
// 3. jmp to the argument
|
|
|
|
TEXT jmpdefer(SB), 7, $0
|
2009-06-03 00:02:12 -06:00
|
|
|
MOVQ 8(SP), AX // fn
|
|
|
|
MOVQ 16(SP), BX // caller sp
|
|
|
|
LEAQ -8(BX), SP // caller sp after CALL
|
|
|
|
SUBQ $5, (SP) // return to CALL again
|
|
|
|
JMP AX // but first run the deferred function
|
2009-10-03 11:37:12 -06:00
|
|
|
|
|
|
|
// runcgo(void(*fn)(void*), void *arg)
|
2009-10-12 11:26:38 -06:00
|
|
|
// Call fn(arg) on the scheduler stack,
|
|
|
|
// aligned appropriately for the gcc ABI.
|
|
|
|
// Save g and m across the call,
|
2009-10-03 11:37:12 -06:00
|
|
|
// since the foreign code might reuse them.
|
|
|
|
TEXT runcgo(SB),7,$32
|
2009-10-12 11:26:38 -06:00
|
|
|
// Save old registers.
|
2009-10-03 11:37:12 -06:00
|
|
|
MOVQ fn+0(FP),AX
|
|
|
|
MOVQ arg+8(FP),DI // DI = first argument in AMD64 ABI
|
|
|
|
MOVQ SP, CX
|
2009-10-12 11:26:38 -06:00
|
|
|
|
|
|
|
// Figure out if we need to switch to m->g0 stack.
|
|
|
|
MOVQ m_g0(m), R8
|
|
|
|
CMPQ R8, g
|
|
|
|
JEQ 2(PC)
|
|
|
|
MOVQ (m_sched+gobuf_sp)(m), SP
|
|
|
|
|
|
|
|
// Now on a scheduling stack (a pthread-created stack).
|
|
|
|
SUBQ $32, SP
|
2009-10-03 11:37:12 -06:00
|
|
|
ANDQ $~15, SP // alignment for gcc ABI
|
|
|
|
MOVQ g, 24(SP) // save old g, m, SP
|
|
|
|
MOVQ m, 16(SP)
|
|
|
|
MOVQ CX, 8(SP)
|
|
|
|
CALL AX
|
2009-10-12 11:26:38 -06:00
|
|
|
|
|
|
|
// Restore registers, stack pointer.
|
|
|
|
MOVQ 16(SP), m
|
2009-10-03 11:37:12 -06:00
|
|
|
MOVQ 24(SP), g
|
|
|
|
MOVQ 8(SP), SP
|
|
|
|
RET
|
|
|
|
|
2009-12-08 19:19:30 -07:00
|
|
|
// check that SP is in range [g->stackbase, g->stackguard)
|
|
|
|
TEXT stackcheck(SB), 7, $0
|
2010-03-30 11:53:16 -06:00
|
|
|
CMPQ g_stackbase(g), SP
|
|
|
|
JHI 2(PC)
|
|
|
|
INT $3
|
|
|
|
CMPQ SP, g_stackguard(g)
|
|
|
|
JHI 2(PC)
|
|
|
|
INT $3
|
|
|
|
RET
|
|
|
|
|
2010-04-05 13:51:09 -06:00
|
|
|
TEXT ·memclr(SB),7,$0
|
|
|
|
MOVQ 8(SP), DI // arg 1 addr
|
|
|
|
MOVL 16(SP), CX // arg 2 count
|
|
|
|
ADDL $7, CX
|
|
|
|
SHRL $3, CX
|
|
|
|
MOVQ $0, AX
|
|
|
|
CLD
|
|
|
|
REP
|
|
|
|
STOSQ
|
|
|
|
RET
|
|
|
|
|
|
|
|
TEXT ·getcallerpc+0(SB),7,$0
|
|
|
|
MOVQ x+0(FP),AX // addr of first arg
|
|
|
|
MOVQ -8(AX),AX // get calling pc
|
|
|
|
RET
|
|
|
|
|
|
|
|
TEXT ·setcallerpc+0(SB),7,$0
|
|
|
|
MOVQ x+0(FP),AX // addr of first arg
|
|
|
|
MOVQ x+8(FP), BX
|
|
|
|
MOVQ BX, -8(AX) // set calling pc
|
|
|
|
RET
|
|
|
|
|
|
|
|
TEXT getcallersp(SB),7,$0
|
|
|
|
MOVQ sp+0(FP), AX
|
|
|
|
RET
|
|
|
|
|