This is the mail archive of the
ecos-patches@sources.redhat.com
mailing list for the eCos project.
Re: [ECOS] Calling exit in a Redboot standalone Arm program
- From: Mark Salter <msalter at redhat dot com>
- To: Pierre dot Habraken at imag dot fr
- Cc: ecos-patches at sources dot redhat dot com
- Date: Tue, 6 May 2003 15:12:04 -0400 (EDT)
- Subject: Re: [ECOS] Calling exit in a Redboot standalone Arm program
- References: <3EB66573.A55B6DA8@imag.fr>
I have been testing Pierre's exit patch and ended up rearranging
some parts of it so that the gdb kill command also uses the same
stub exit mechanism. Also, I realized that the call_breakpoint
wrapper was not necessary as I originally thought because the
existing trampoline function already wrapped the target function
in the HAL_ARCH_PROGRAM_NEW_STACK macro. Anyway, here is what the
reworked patch looks like. I'll probably check it in today along
with Pierre's "step into swi" and usermode patches.
--Mark
Index: hal/arm/arch/current/ChangeLog
===================================================================
RCS file: /cvs/ecos/ecos/packages/hal/arm/arch/current/ChangeLog,v
retrieving revision 1.89
diff -u -p -5 -r1.89 ChangeLog
--- hal/arm/arch/current/ChangeLog 30 Apr 2003 20:02:41 -0000 1.89
+++ hal/arm/arch/current/ChangeLog 6 May 2003 19:00:12 -0000
@@ -1,5 +1,10 @@
+2003-05-06 Pierre Habraken <Pierre.Habraken@imag.fr>
+
+ * src/arm_stub.c (target_ins, target_thumb_ins): Added code to
+ enable stepping into swi instructions.
+
2003-04-30 Jonathan Larmour <jifl@eCosCentric.com>
* src/vectors.S (handle_IRQ_or_FIQ): Tweak below to be conditional on
__thumb__.
Index: hal/arm/arch/current/src/arm_stub.c
===================================================================
RCS file: /cvs/ecos/ecos/packages/hal/arm/arch/current/src/arm_stub.c,v
retrieving revision 1.26
diff -u -p -5 -r1.26 arm_stub.c
--- hal/arm/arch/current/src/arm_stub.c 12 Nov 2002 21:50:39 -0000 1.26
+++ hal/arm/arch/current/src/arm_stub.c 6 May 2003 19:00:12 -0000
@@ -6,11 +6,11 @@
//
//========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
-// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
+// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
//
// eCos 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 or (at your option) any later version.
//
@@ -526,11 +526,16 @@ target_ins(unsigned long *pc, unsigned l
// Falls through
return (pc+1);
}
}
case 0x3: // Coprocessor & SWI
- return (pc+1);
+ if (((ins & 0x03000000) == 0x03000000) && ins_will_execute(ins)) {
+ // SWI
+ return (CYGNUM_HAL_VECTOR_SOFTWARE_INTERRUPT * 4);
+ } else {
+ return (pc+1);
+ }
default:
// Never reached - but fixes compiler warning.
return 0;
}
}
@@ -568,14 +573,17 @@ target_thumb_ins(unsigned long pc, unsig
if (!v5T_semantics())
new_pc = MAKE_THUMB_ADDR(new_pc);
}
break;
case 0xd:
- // Bcc
+ // Bcc | SWI
// Use ARM function to check condition
arm_ins = ((unsigned long)(ins & 0x0f00)) << 20;
- if (ins_will_execute(arm_ins)) {
+ if ((arm_ins & 0xF0000000) == 0xF0000000) {
+ // SWI
+ new_pc = CYGNUM_HAL_VECTOR_SOFTWARE_INTERRUPT * 4;
+ } else if (ins_will_execute(arm_ins)) {
offset = (ins & 0x00FF) << 1;
if (ins & 0x0080) offset |= 0xFFFFFE00; // sign extend
new_pc = MAKE_THUMB_ADDR((unsigned long)(pc+4) + offset);
}
break;
Index: hal/common/current/ChangeLog
===================================================================
RCS file: /cvs/ecos/ecos/packages/hal/common/current/ChangeLog,v
retrieving revision 1.91
diff -u -p -5 -r1.91 ChangeLog
--- hal/common/current/ChangeLog 8 Apr 2003 17:14:58 -0000 1.91
+++ hal/common/current/ChangeLog 6 May 2003 19:00:15 -0000
@@ -1,5 +1,15 @@
+2003-05-06 Mark Salter <msalter@redhat.com>
+
+ * src/hal_stub.c (handle_exception_exit): Call sys_profile_reset from
+ here. Setup to return through return_from_stub() when appropriate.
+ (return_from_stub): New function to call CYGACC_CALL_IF_MONITOR_RETURN
+ from thread context.
+
+ * src/generic-stub.c (__handle_exception): Call exit_vec if
+ hal_syscall_handler returns negative number.
+
2003-04-08 Mark Salter <msalter@redhat.com>
* src/hal_misc.c (hal_default_isr): Allow HAL to override default
GDB vector matching test.
Index: hal/common/current/src/generic-stub.c
===================================================================
RCS file: /cvs/ecos/ecos/packages/hal/common/current/src/generic-stub.c,v
retrieving revision 1.31
diff -u -p -5 -r1.31 generic-stub.c
--- hal/common/current/src/generic-stub.c 8 Apr 2003 01:13:15 -0000 1.31
+++ hal/common/current/src/generic-stub.c 6 May 2003 19:00:15 -0000
@@ -905,12 +905,15 @@ __handle_exception (void)
#if defined(CYGSEM_REDBOOT_BSP_SYSCALLS)
// Temporary support for gnupro bsp SWIs
if (__is_bsp_syscall())
{
sigval = hal_syscall_handler();
- if (0 == sigval)
+ if (sigval <= 0)
{
+ if (sigval < 0)
+ __process_exit_vec ();
+
if (__init_vec != NULL)
__init_vec ();
return;
}
}
@@ -1434,16 +1437,10 @@ __process_packet (char *packet)
/* fall through */
case 'k' : /* kill the program */
#ifdef __ECOS__
hal_flush_output();
#endif
-#ifdef CYGSEM_REDBOOT_BSP_SYSCALLS_GPROF
- { // Reset the timer to default and cancel any callback
- extern void sys_profile_reset(void);
- sys_profile_reset();
- }
-#endif // CYGSEM_REDBOOT_BSP_SYSCALLS_GPROF
__process_exit_vec ();
return -1;
case 'r': /* Reset */
/* With the next 'k' packet, reset the board */
Index: hal/common/current/src/hal_stub.c
===================================================================
RCS file: /cvs/ecos/ecos/packages/hal/common/current/src/hal_stub.c,v
retrieving revision 1.37
diff -u -p -5 -r1.37 hal_stub.c
--- hal/common/current/src/hal_stub.c 23 May 2002 23:02:49 -0000 1.37
+++ hal/common/current/src/hal_stub.c 6 May 2003 19:00:17 -0000
@@ -6,11 +6,11 @@
//
//=============================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
-// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
+// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
//
// eCos 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 or (at your option) any later version.
//
@@ -95,11 +95,13 @@
// Saved registers.
HAL_SavedRegisters *_hal_registers;
target_register_t registers[HAL_STUB_REGISTERS_SIZE];
target_register_t alt_registers[HAL_STUB_REGISTERS_SIZE] ; // Thread or saved process state
target_register_t * _registers = registers; // Pointer to current set of registers
+#ifndef CYGPKG_REDBOOT
target_register_t orig_registers[HAL_STUB_REGISTERS_SIZE]; // Registers to get back to original state
+#endif
#if defined(HAL_STUB_HW_WATCHPOINT) || defined(HAL_STUB_HW_BREAKPOINT)
static int _hw_stop_reason; // Reason we were stopped by hw.
//#define HAL_STUB_HW_SEND_STOP_REASON_TEXT
@@ -376,43 +378,66 @@ interruptible(int state)
#ifdef CYGDBG_HAL_DEBUG_GDB_BREAK_SUPPORT
int cyg_hal_gdb_break;
#endif
+#ifdef CYGPKG_REDBOOT
+// Trampoline for returning to RedBoot from exception/stub code
+static void
+return_from_stub(int exit_status)
+{
+ CYGACC_CALL_IF_MONITOR_RETURN(exit_status);
+}
+#endif
+
// Called at stub *kill*
static void
handle_exception_exit( void )
{
+#ifdef CYGPKG_REDBOOT
+#ifdef CYGSEM_REDBOOT_BSP_SYSCALLS_GPROF
+ { // Reset the timer to default and cancel any callback
+ extern void sys_profile_reset(void);
+ sys_profile_reset();
+ }
+#endif // CYGSEM_REDBOOT_BSP_SYSCALLS_GPROF
+ set_pc((target_register_t)return_from_stub);
+#else
int i;
for (i = 0; i < (sizeof(registers)/sizeof(registers[0])); i++)
registers[i] = orig_registers[i];
+#endif
}
// Called at stub *entry*
static void
handle_exception_cleanup( void )
{
+#ifndef CYGPKG_REDBOOT
static int orig_registers_set = 0;
+#endif
interruptible(0);
// Expand the HAL_SavedRegisters structure into the GDB register
// array format.
HAL_GET_GDB_REGISTERS(®isters[0], _hal_registers);
_registers = ®isters[0];
+#ifndef CYGPKG_REDBOOT
if (!orig_registers_set) {
int i;
for (i = 0; i < (sizeof(registers)/sizeof(registers[0])); i++)
orig_registers[i] = registers[i];
_registers = &orig_registers[0];
if (__is_breakpoint_function ())
__skipinst ();
_registers = ®isters[0];
orig_registers_set = 1;
}
+#endif
#ifdef HAL_STUB_PLATFORM_STUBS_FIXUP
// Some architectures may need to fix the PC in case of a partial
// or fully executed trap instruction. GDB only takes correct action
// when the PC is pointing to the breakpoint instruction it set.
Index: redboot/current/ChangeLog
===================================================================
RCS file: /cvs/ecos/ecos/packages/redboot/current/ChangeLog,v
retrieving revision 1.107
diff -u -p -5 -r1.107 ChangeLog
--- redboot/current/ChangeLog 24 Apr 2003 03:33:04 -0000 1.107
+++ redboot/current/ChangeLog 6 May 2003 19:00:24 -0000
@@ -1,5 +1,28 @@
+2003-05-06 Mark Salter <msalter@redhat.com>
+
+ * src/main.c (cyg_start): Clear gdb_active when returning from stub.
+
+2003-05-06 Pierre Habraken <Pierre.Habraken@imag.fr>
+
+ * cdl/redboot.cdl: Added option
+ CYGOPT_REDBOOT_BSP_SYSCALLS_EXIT_WITHOUT_TRAP for enabling (possibly
+ single shot) programs to exit and return a termination status as
+ their normal behavior (i.e. without raising a SIGTRAP).
+ * src/syscall.c (__do_syscall): Added conditionally compiled code
+ to SYS_exit for returning to RedBoot main loop without raising a
+ SIGTRAP, even when the calling program is not being debugged.
+ * src/main.c (cyg_start): Added code (following Jonathan Larmour's
+ and Mark Salter's suggestions) to RedBoot main loop: a (context)
+ savepoint is created before the thread of control is transferred to
+ gdb stubs (through a trampoline procedure). This savepoint enables
+ the stubs to return control back to the main loop by calling the
+ return_to_redboot procedure (macro CYGACC_CALL_IF_MONITOR_RETURN).
+ Procedure go_trampoline and variables go_saved_context and
+ go_return_status have been respectively renamed to trampoline,
+ saved_context and return_status.
+
2003-04-23 Chris Garry <cgarry@sweeneydesign.co.uk>
* src/flash.c: Only perform RAM check in fis load command when
CYGSEM_REDBOOT_VALIDATE_USER_RAM_LOADS = 1
Index: redboot/current/cdl/redboot.cdl
===================================================================
RCS file: /cvs/ecos/ecos/packages/redboot/current/cdl/redboot.cdl,v
retrieving revision 1.47
diff -u -p -5 -r1.47 redboot.cdl
--- redboot/current/cdl/redboot.cdl 28 Mar 2003 13:49:32 -0000 1.47
+++ redboot/current/cdl/redboot.cdl 6 May 2003 19:00:24 -0000
@@ -859,10 +859,19 @@ cdl_package CYGPKG_REDBOOT {
cdl_interface CYGINT_REDBOOT_BSP_SYSCALLS_GPROF_SUPPORT {
display "Does the HAL support 'gprof' profiling?"
no_define
}
+
+ cdl_option CYGOPT_REDBOOT_BSP_SYSCALLS_EXIT_WITHOUT_TRAP {
+ display "Do not raise SIGTRAP when program exits"
+ default_value 0
+ description "
+ For some (single shot) newlib based programs,
+ exiting and returning a termination status may be
+ the normal expected behavior."
+ }
}
cdl_component CYGOPT_REDBOOT_FIS_ZLIB_COMMON_BUFFER {
display "Use a common buffer for Zlib and FIS"
flavor bool
Index: redboot/current/src/main.c
===================================================================
RCS file: /cvs/ecos/ecos/packages/redboot/current/src/main.c,v
retrieving revision 1.44
diff -u -p -5 -r1.44 main.c
--- redboot/current/src/main.c 8 Apr 2003 05:09:06 -0000 1.44
+++ redboot/current/src/main.c 6 May 2003 19:00:24 -0000
@@ -78,13 +78,23 @@ extern void breakpoint(void);
#endif
// Builtin Self Test (BIST)
externC void bist(void);
-// Return path for code run from a go command
+// Path to code run from a go command or to GDB stubs
+static void trampoline(unsigned long entry);
+
+// Return path for code run from a go command or for GDB stubs
static void return_to_redboot(int status);
+// Address of area where current context is saved before executing
+// trampoline procedure
+static void * saved_context;
+
+// Status returned after trampoline execution
+static int return_status;
+
// CLI command processing (defined in this file)
RedBoot_cmd("version",
"Display RedBoot version information",
"",
@@ -314,15 +324,20 @@ cyg_start(void)
__chan = CYGACC_CALL_IF_CONSOLE_PROCS();
CYGACC_COMM_IF_CONTROL( *__chan, __COMMCTL_IRQ_DISABLE );
}
CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
-#ifdef HAL_ARCH_PROGRAM_NEW_STACK
- HAL_ARCH_PROGRAM_NEW_STACK(breakpoint);
-#else
- breakpoint(); // Get GDB stubs started, with a proper environment, etc.
-#endif
+
+ // set up a temporary context that will take us to the trampoline
+ HAL_THREAD_INIT_CONTEXT((CYG_ADDRESS)workspace_end,
+ breakpoint, trampoline, 0);
+
+ // switch context to trampoline (get GDB stubs started)
+ HAL_THREAD_SWITCH_CONTEXT(&saved_context, &workspace_end);
+
+ gdb_active = false;
+
dbgchan = CYGACC_CALL_IF_SET_DEBUG_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
CYGACC_CALL_IF_SET_CONSOLE_COMM(dbgchan);
} else
#endif // CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
{
@@ -386,15 +401,12 @@ do_help(int argc, char *argv[])
cmd = __RedBoot_CMD_TAB__;
show_help(cmd, &__RedBoot_CMD_TAB_END__, which, "");
return;
}
-static void * go_saved_context;
-static int go_return_status;
-
static void
-go_trampoline(unsigned long entry)
+trampoline(unsigned long entry)
{
typedef void code_fun(void);
code_fun *fun = (code_fun *)entry;
unsigned long oldints;
@@ -403,19 +415,21 @@ go_trampoline(unsigned long entry)
#ifdef HAL_ARCH_PROGRAM_NEW_STACK
HAL_ARCH_PROGRAM_NEW_STACK(fun);
#else
(*fun)();
#endif
+
+ HAL_THREAD_LOAD_CONTEXT(&saved_context);
}
static void
return_to_redboot(int status)
{
CYGARC_HAL_SAVE_GP();
- go_return_status = status;
- HAL_THREAD_LOAD_CONTEXT(&go_saved_context);
+ return_status = status;
+ HAL_THREAD_LOAD_CONTEXT(&saved_context);
// never returns
// need this to balance above CYGARC_HAL_SAVE_GP on
// some platforms. It will never run, though.
CYGARC_HAL_RESTORE_GP();
@@ -498,14 +512,14 @@ do_go(int argc, char *argv[])
}
HAL_ICACHE_INVALIDATE_ALL();
HAL_DCACHE_INVALIDATE_ALL();
// set up a temporary context that will take us to the trampoline
- HAL_THREAD_INIT_CONTEXT((CYG_ADDRESS)workspace_end, entry, go_trampoline, 0);
+ HAL_THREAD_INIT_CONTEXT((CYG_ADDRESS)workspace_end, entry, trampoline, 0);
// switch context to trampoline
- HAL_THREAD_SWITCH_CONTEXT(&go_saved_context, &workspace_end);
+ HAL_THREAD_SWITCH_CONTEXT(&saved_context, &workspace_end);
// we get back here by way of return_to_redboot()
// undo the changes we made before switching context
if (!cache_enabled) {
@@ -515,11 +529,11 @@ do_go(int argc, char *argv[])
CYGACC_COMM_IF_CONTROL(*__chan, __COMMCTL_DISABLE_LINE_FLUSH);
HAL_RESTORE_INTERRUPTS(oldints);
- diag_printf("\nProgram completed with status %d\n", go_return_status);
+ diag_printf("\nProgram completed with status %d\n", return_status);
}
#ifdef HAL_PLATFORM_RESET
void
do_reset(int argc, char *argv[])
Index: redboot/current/src/syscall.c
===================================================================
RCS file: /cvs/ecos/ecos/packages/redboot/current/src/syscall.c,v
retrieving revision 1.9
diff -u -p -5 -r1.9 syscall.c
--- redboot/current/src/syscall.c 31 Jan 2003 16:46:01 -0000 1.9
+++ redboot/current/src/syscall.c 6 May 2003 19:00:24 -0000
@@ -629,16 +629,20 @@ __do_syscall(CYG_ADDRWORD func,
case __GET_SHARED:
*(__shared_t **)arg1 = &__shared_data;
break;
case SYS_exit:
+ *sig = -1; // signal exit
+ err = arg1;
+
if (gdb_active) {
+#ifdef CYGOPT_REDBOOT_BSP_SYSCALLS_EXIT_WITHOUT_TRAP
+ __send_exit_status((int)arg1);
+#else
*sig = SIGTRAP;
err = func;
- } else {
- CYGACC_CALL_IF_MONITOR_RETURN(arg1);
- // never returns
+#endif // CYGOPT_REDBOOT_BSP_SYSCALLS_EXIT_WITHOUT_TRAP
}
break;
default:
return 0;