2011-11-14 21:58:11 +00:00
|
|
|
/* machdep.S - machine dependent assembly routines for the GDB stub */
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Lubomir Kundrak
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/cpu/gdb.h>
|
2012-02-29 13:44:52 +00:00
|
|
|
#include <grub/symbol.h>
|
2011-11-14 21:58:11 +00:00
|
|
|
|
|
|
|
#define EC_PRESENT 1
|
|
|
|
#define EC_ABSENT 0
|
|
|
|
|
|
|
|
#define GRUB_GDB_STACKSIZE 40000
|
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
#define SEP ,
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
.zerofill __DATA, __bss, LOCAL(grub_gdb_stack_end), GRUB_GDB_STACKSIZE, 4
|
|
|
|
LOCAL(grub_gdb_stack) = LOCAL(grub_gdb_stack_end)
|
|
|
|
#else
|
2011-11-14 21:58:11 +00:00
|
|
|
/*
|
|
|
|
* The .data index for the address vector.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define VECTOR 1
|
|
|
|
|
|
|
|
.bss
|
|
|
|
.space GRUB_GDB_STACKSIZE
|
2012-02-29 13:44:52 +00:00
|
|
|
VARIABLE(grub_gdb_stack)
|
2012-05-29 11:52:26 +00:00
|
|
|
#endif
|
2011-11-14 21:58:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Supplemental macros for register saving/restoration
|
|
|
|
* on exception handler entry/leave.
|
|
|
|
*/
|
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
.macro save32
|
|
|
|
#define REG $0
|
|
|
|
#define NDX $1
|
|
|
|
#else
|
2011-11-14 21:58:11 +00:00
|
|
|
.macro save32 reg ndx
|
2012-05-29 11:52:26 +00:00
|
|
|
#define REG \reg
|
|
|
|
#define NDX \ndx
|
|
|
|
#endif
|
|
|
|
movl REG, EXT_C(grub_gdb_regs)+(NDX * 4)
|
2011-11-14 21:58:11 +00:00
|
|
|
.endm
|
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
#undef REG
|
|
|
|
#undef NDX
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
.macro save16
|
|
|
|
#define REG $0
|
|
|
|
#define NDX $1
|
|
|
|
#else
|
2011-11-14 21:58:11 +00:00
|
|
|
.macro save16 reg ndx
|
2012-05-29 11:52:26 +00:00
|
|
|
#define REG \reg
|
|
|
|
#define NDX \ndx
|
|
|
|
#endif
|
|
|
|
#ifdef __APPLE__
|
|
|
|
xorl %eax, %eax
|
|
|
|
#else
|
|
|
|
movl $0, %eax
|
|
|
|
#endif
|
|
|
|
movw REG, EXT_C(grub_gdb_regs)+(NDX * 4)
|
|
|
|
movw %ax, EXT_C(grub_gdb_regs)+(NDX * 4 + 2)
|
2012-02-29 13:44:52 +00:00
|
|
|
movl EXT_C(grub_gdb_regs)+(EAX * 4), %eax
|
2011-11-14 21:58:11 +00:00
|
|
|
.endm
|
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
#undef REG
|
|
|
|
#undef NDX
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
.macro load32
|
|
|
|
#define NDX $0
|
|
|
|
#define REG $1
|
|
|
|
#else
|
2011-11-14 21:58:11 +00:00
|
|
|
.macro load32 ndx reg
|
2012-05-29 11:52:26 +00:00
|
|
|
#define REG \reg
|
|
|
|
#define NDX \ndx
|
|
|
|
#endif
|
|
|
|
movl EXT_C(grub_gdb_regs)+(NDX * 4), REG
|
2011-11-14 21:58:11 +00:00
|
|
|
.endm
|
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
#undef REG
|
|
|
|
#undef NDX
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
.macro load16
|
|
|
|
#define NDX $0
|
|
|
|
#define REG $1
|
|
|
|
#else
|
|
|
|
.macro load16 ndx reg
|
|
|
|
#define NDX \ndx
|
|
|
|
#define REG \reg
|
|
|
|
#endif
|
|
|
|
movw EXT_C(grub_gdb_regs)+(NDX * 4), REG
|
2011-11-14 21:58:11 +00:00
|
|
|
.endm
|
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
#undef REG
|
|
|
|
#undef NDX
|
|
|
|
|
2011-11-14 21:58:11 +00:00
|
|
|
.macro save_context
|
2012-05-29 11:52:26 +00:00
|
|
|
save32 %eax, EAX
|
|
|
|
|
|
|
|
save32 %ecx, ECX
|
|
|
|
save32 %edx, EDX
|
|
|
|
save32 %ebx, EBX
|
|
|
|
save32 %ebp, EBP
|
|
|
|
save32 %esi, ESI
|
|
|
|
save32 %edi, EDI
|
2011-11-14 21:58:11 +00:00
|
|
|
|
|
|
|
popl %ebx
|
2012-05-29 11:52:26 +00:00
|
|
|
save32 %ebx, EIP
|
2011-11-14 21:58:11 +00:00
|
|
|
popl %ebx
|
2012-05-29 11:52:26 +00:00
|
|
|
save32 %ebx, CS
|
2011-11-14 21:58:11 +00:00
|
|
|
popl %ebx
|
2012-05-29 11:52:26 +00:00
|
|
|
save32 %ebx, EFLAGS
|
2011-11-14 21:58:11 +00:00
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
save32 %esp, ESP
|
2011-11-14 21:58:11 +00:00
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
save16 %ds, DS
|
|
|
|
save16 %es, ES
|
|
|
|
save16 %fs, FS
|
|
|
|
save16 %gs, GS
|
|
|
|
save16 %ss, SS
|
2011-11-14 21:58:11 +00:00
|
|
|
.endm
|
|
|
|
|
|
|
|
.macro load_context
|
2012-05-29 11:52:26 +00:00
|
|
|
load16 SS, %ss
|
|
|
|
load32 ESP, %esp
|
2011-11-14 21:58:11 +00:00
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
load32 EBP, %ebp
|
|
|
|
load32 ESI, %esi
|
|
|
|
load32 EDI, %edi
|
2011-11-14 21:58:11 +00:00
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
load16 DS, %ds
|
|
|
|
load16 ES, %es
|
|
|
|
load16 FS, %fs
|
|
|
|
load16 GS, %gs
|
2011-11-14 21:58:11 +00:00
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
load32 EFLAGS, %eax
|
2011-11-14 21:58:11 +00:00
|
|
|
pushl %eax
|
2012-05-29 11:52:26 +00:00
|
|
|
load32 CS, %eax
|
2011-11-14 21:58:11 +00:00
|
|
|
pushl %eax
|
2012-05-29 11:52:26 +00:00
|
|
|
load32 EIP, %eax
|
2011-11-14 21:58:11 +00:00
|
|
|
pushl %eax
|
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
load32 EBX, %ebx
|
|
|
|
load32 EDX, %edx
|
|
|
|
load32 ECX, %ecx
|
|
|
|
load32 EAX, %eax
|
2011-11-14 21:58:11 +00:00
|
|
|
.endm
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This macro creates handlers for a given range of exception numbers
|
|
|
|
* and adds their addresses to the grub_gdb_trapvec array.
|
|
|
|
*/
|
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
.macro ent
|
|
|
|
#define EC $0
|
|
|
|
#define BEG $1
|
|
|
|
#define END $2
|
|
|
|
#else
|
2011-11-14 21:58:11 +00:00
|
|
|
.macro ent ec beg end=0
|
2012-05-29 11:52:26 +00:00
|
|
|
#define EC \ec
|
|
|
|
#define BEG \beg
|
|
|
|
#define END \end
|
|
|
|
#endif
|
2011-11-14 21:58:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapper body itself.
|
|
|
|
*/
|
|
|
|
|
|
|
|
.text
|
|
|
|
1:
|
2012-05-29 11:52:26 +00:00
|
|
|
.if EC
|
|
|
|
#ifdef __APPLE__
|
|
|
|
add $$4, %esp
|
|
|
|
#else
|
|
|
|
add $4, %esp
|
|
|
|
#endif
|
2011-11-14 21:58:11 +00:00
|
|
|
.endif
|
|
|
|
|
|
|
|
save_context
|
2012-05-29 11:52:26 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
mov $LOCAL(grub_gdb_stack), %esp
|
|
|
|
#else
|
2012-02-29 13:44:52 +00:00
|
|
|
mov $EXT_C(grub_gdb_stack), %esp
|
2012-05-29 11:52:26 +00:00
|
|
|
#endif
|
|
|
|
mov $(BEG), %eax /* trap number */
|
2012-02-29 13:44:52 +00:00
|
|
|
call EXT_C(grub_gdb_trap)
|
2011-11-14 21:58:11 +00:00
|
|
|
load_context
|
|
|
|
iret
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Address entry in trapvec array.
|
|
|
|
*/
|
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
.section __DATA, VECTOR
|
|
|
|
#else
|
|
|
|
.data VECTOR
|
|
|
|
#endif
|
2011-11-14 21:58:11 +00:00
|
|
|
.long 1b
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Next... (recursion).
|
|
|
|
*/
|
|
|
|
|
2012-05-29 11:52:26 +00:00
|
|
|
.if END-BEG > 0
|
|
|
|
#ifdef __APPLE__
|
|
|
|
ent EC, (BEG+1), END
|
|
|
|
#else
|
2011-11-14 21:58:11 +00:00
|
|
|
ent \ec "(\beg+1)" \end
|
2012-05-29 11:52:26 +00:00
|
|
|
#endif
|
2011-11-14 21:58:11 +00:00
|
|
|
.endif
|
|
|
|
.endm
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here does the actual construction of the address array and handlers
|
|
|
|
* take place.
|
|
|
|
*/
|
2012-05-29 11:52:26 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
.section __DATA, VECTOR
|
|
|
|
#else
|
|
|
|
.data VECTOR
|
|
|
|
#endif
|
2012-02-29 13:44:52 +00:00
|
|
|
VARIABLE(grub_gdb_trapvec)
|
2012-05-29 11:52:26 +00:00
|
|
|
ent EC_ABSENT, 0, 7
|
|
|
|
ent EC_PRESENT, 8
|
|
|
|
ent EC_ABSENT, 9
|
|
|
|
ent EC_PRESENT, 10, 14
|
2011-11-14 21:58:11 +00:00
|
|
|
/*
|
|
|
|
* You may have to split this further or as(1)
|
|
|
|
* will complain about nesting being too deep.
|
|
|
|
*/
|
2012-05-29 11:52:26 +00:00
|
|
|
ent EC_ABSENT, 15, GRUB_GDB_LAST_TRAP
|