Details of Syscall.RawSyscall() & Syscall.Syscall() in Go?

北城以北 提交于 2019-12-01 08:55:55

I'll share my reduced assembly knowledge with you:

61  TEXT ·RawSyscall(SB),7,$0
62      MOVQ    16(SP), DI
63      MOVQ    24(SP), SI
64      MOVQ    32(SP), DX
65      MOVQ    $0, R10
66      MOVQ    $0, R8
67      MOVQ    $0, R9
68      MOVQ    8(SP), AX   // syscall entry
69      ADDQ    $0x2000000, AX
70      SYSCALL
71      JCC ok1
72      MOVQ    $-1, 40(SP) // r1
73      MOVQ    $0, 48(SP)  // r2
74      MOVQ    AX, 56(SP)  // errno
75      RET
76  ok1:
77      MOVQ    AX, 40(SP)  // r1
78      MOVQ    DX, 48(SP)  // r2
79      MOVQ    $0, 56(SP)  // errno
80      RET
81  
  • Line 61 is the routine entry point
  • Line 76 is a label called ok1
  • Line 71 is a conditional jump to label ok1.

The short names you see on every line on the left side are called mnemonics and stand for assembly instructions:

  • MOVQ means Move Quadword (64 bits of data).
  • ADDQ is Add Quadword.
  • SYSCALL is kinda obvious
  • JCC is Jump if Condition (condition flag set by previous instruction)
  • RET is return

On the right side of the mnemonics you'll find each instruction's arguments which are basically constants and registers.

  • SP is the Stack Pointer
  • AX is the Accumulator
  • BX is the Base register

each register can hold a certain amount of data. On 64 bit CPU architectures I believe it's in fact 64 bits per register.

The only difference between Syscall and RawSyscall is on line 14, 28 and 34 where Syscall will call runtime·entersyscall(SB) and runtime·exitsyscall(SB) whereas RawSyscall will not. I assume this means that Syscall notifies the runtime that it's switched to a blocking syscall operations and can yield CPU-time to another goroutine/thread whereas RawSyscall will just block.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!