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
|
|
|
|
MOVQ 0(SP), AX // argc
|
|
|
|
LEAQ 8(SP), BX // argv
|
|
|
|
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
|
2008-07-11 20:16:39 -06:00
|
|
|
CALL sys·newproc(SB)
|
|
|
|
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.
|
|
|
|
TEXT sys·morestack(SB),7,$0
|
|
|
|
// 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.
|
|
|
|
// We set f's frame size to zero, meaning
|
|
|
|
// allocate a standard sized stack segment.
|
|
|
|
// If it turns out that f needs a larger frame than this,
|
|
|
|
// f's usual stack growth prolog will allocate
|
|
|
|
// a new segment (and recopy the arguments).
|
|
|
|
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
|
|
|
|
MOVL $0, m_moreframe(m) // f's frame size
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
TEXT sys·lessstack(SB), 7, $0
|
|
|
|
// 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
|
|
|
|
TEXT sys·morestack00+0(SB),7,$0
|
|
|
|
MOVQ $0, AX
|
2009-06-17 17:31:02 -06:00
|
|
|
MOVQ AX, m_moreframe(m)
|
2009-05-01 19:07:33 -06:00
|
|
|
MOVQ $sys·morestack+0(SB), AX
|
|
|
|
JMP AX
|
|
|
|
|
|
|
|
TEXT sys·morestack01+0(SB),7,$0
|
|
|
|
SHLQ $32, AX
|
2009-06-17 17:31:02 -06:00
|
|
|
MOVQ AX, m_moreframe(m)
|
2009-05-01 19:07:33 -06:00
|
|
|
MOVQ $sys·morestack+0(SB), AX
|
|
|
|
JMP AX
|
|
|
|
|
|
|
|
TEXT sys·morestack10+0(SB),7,$0
|
|
|
|
MOVLQZX AX, AX
|
2009-06-17 17:31:02 -06:00
|
|
|
MOVQ AX, m_moreframe(m)
|
2009-05-01 19:07:33 -06:00
|
|
|
MOVQ $sys·morestack+0(SB), AX
|
|
|
|
JMP AX
|
|
|
|
|
|
|
|
TEXT sys·morestack11+0(SB),7,$0
|
2009-06-17 17:31:02 -06:00
|
|
|
MOVQ AX, m_moreframe(m)
|
2009-05-01 19:07:33 -06:00
|
|
|
MOVQ $sys·morestack+0(SB), AX
|
|
|
|
JMP AX
|
|
|
|
|
2009-05-03 20:09:14 -06:00
|
|
|
// subcases of morestack01
|
|
|
|
// with const of 8,16,...48
|
|
|
|
TEXT sys·morestack8(SB),7,$0
|
|
|
|
PUSHQ $1
|
|
|
|
MOVQ $sys·morestackx(SB), AX
|
|
|
|
JMP AX
|
|
|
|
|
|
|
|
TEXT sys·morestack16(SB),7,$0
|
|
|
|
PUSHQ $2
|
|
|
|
MOVQ $sys·morestackx(SB), AX
|
|
|
|
JMP AX
|
|
|
|
|
|
|
|
TEXT sys·morestack24(SB),7,$0
|
|
|
|
PUSHQ $3
|
|
|
|
MOVQ $sys·morestackx(SB), AX
|
|
|
|
JMP AX
|
|
|
|
|
|
|
|
TEXT sys·morestack32(SB),7,$0
|
|
|
|
PUSHQ $4
|
|
|
|
MOVQ $sys·morestackx(SB), AX
|
|
|
|
JMP AX
|
|
|
|
|
|
|
|
TEXT sys·morestack40(SB),7,$0
|
|
|
|
PUSHQ $5
|
|
|
|
MOVQ $sys·morestackx(SB), AX
|
|
|
|
JMP AX
|
|
|
|
|
|
|
|
TEXT sys·morestack48(SB),7,$0
|
|
|
|
PUSHQ $6
|
|
|
|
MOVQ $sys·morestackx(SB), AX
|
|
|
|
JMP AX
|
|
|
|
|
2009-06-17 16:12:16 -06:00
|
|
|
TEXT sys·morestackx(SB),7,$0
|
|
|
|
POPQ AX
|
|
|
|
SHLQ $35, AX
|
2009-06-17 17:31:02 -06:00
|
|
|
MOVQ AX, m_moreframe(m)
|
2009-06-17 16:12:16 -06:00
|
|
|
MOVQ $sys·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)
|
|
|
|
// Call fn(arg), but align the stack
|
|
|
|
// appropriately for the gcc ABI
|
|
|
|
// and also save g and m across the call,
|
|
|
|
// since the foreign code might reuse them.
|
|
|
|
TEXT runcgo(SB),7,$32
|
|
|
|
MOVQ fn+0(FP),AX
|
|
|
|
MOVQ arg+8(FP),DI // DI = first argument in AMD64 ABI
|
|
|
|
MOVQ SP, CX
|
|
|
|
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
|
|
|
|
MOVQ 16(SP), m // restore
|
|
|
|
MOVQ 24(SP), g
|
|
|
|
MOVQ 8(SP), SP
|
|
|
|
RET
|
|
|
|
|