Subversion Repositories HelenOS

Rev

Rev 2935 | Rev 2941 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Jiri Svoboda
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup debug
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <sys/types.h>
  38. #include <udebug.h>
  39.  
  40. #include "../../../cons.h"
  41. #include "../../../main.h"
  42. #include "../../../include/arch.h"
  43.  
  44. #define OPCODE_INT3     0xCC
  45.  
  46. #define ISTATE_OFF_EIP      12
  47. #define ISTATE_OFF_EFLAGS   14
  48.  
  49. void arch_breakpoint_add(uintptr_t addr)
  50. {
  51.     char brkp[1];
  52.     int rc;
  53.     breakpoint_t *brk;
  54.     int i;
  55.  
  56.     brk = NULL;
  57.     for (i = 0; i < MAX_BRKPTS; i++)
  58.         if (brk_list[i].set == 0) brk = brk_list+i;
  59.  
  60.     if (!brk) {
  61.         cons_printf("too many breakpoints\n");
  62.         return;
  63.     }
  64.  
  65.     rc = udebug_mem_read(app_phone, &brk->arch.back, addr, 1);
  66.     cons_printf("udebug_mem_read() -> %d\n", rc);
  67.     brkp[0] = OPCODE_INT3;
  68.     rc = udebug_mem_write(app_phone, brkp, addr, 1);
  69.     cons_printf("udebug_mem_write() -> %d\n", rc);
  70.  
  71.     brk->addr = addr;
  72.     brk->set = 1;
  73. }
  74.  
  75. static unsigned buffer[1024];
  76. static breakpoint_t *lifted_brkpt;
  77.  
  78. void arch_event_breakpoint(thash_t thread_hash)
  79. {
  80.     int rc;
  81.  
  82.     rc = udebug_regs_read(app_phone, thread_hash, buffer);
  83.     cons_printf("udebug_regs_read -> %d\n", rc);
  84.     cons_printf("EIP was 0x%08x\n", buffer[ISTATE_OFF_EIP]);
  85.     int brk_addr = buffer[ISTATE_OFF_EIP] - 1;
  86.     int bi;
  87.     for (bi = 0; bi < MAX_BRKPTS; bi++) {
  88.         if (brk_list[bi].set && brk_list[bi].addr == brk_addr)
  89.             break;
  90.     }
  91.  
  92.     if (bi < MAX_BRKPTS) {
  93.         cons_printf("breakpoint %d hit\n", bi);
  94.         breakpoint_hit();
  95.  
  96.         buffer[ISTATE_OFF_EIP] = brk_addr;
  97.         buffer[ISTATE_OFF_EFLAGS] |= 0x0100; /* trap flag */
  98.         cons_printf("setting EIP to 0x%08x\n", buffer[ISTATE_OFF_EIP]);
  99.         rc = udebug_regs_write(app_phone, thread_hash, buffer);
  100.             rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 1);
  101.         cons_printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc);
  102.         lifted_brkpt = &brk_list[bi];
  103.     } else {
  104.         cons_printf("unrecognized breakpoint at 0x%x\n", brk_addr);
  105.     }
  106. }
  107.  
  108. void arch_event_trap(thash_t thread_hash)
  109. {
  110.     unsigned char brkinstr[1];
  111.     int rc;
  112.  
  113.     cons_printf("trap event\n");
  114.  
  115.     breakpoint_t *lb = lifted_brkpt;
  116.     brkinstr[0] = OPCODE_INT3;
  117.     rc = udebug_mem_write(app_phone, brkinstr, lb->addr, 1);
  118.     cons_printf("restore breakpoint -> %d\n", rc);
  119.  
  120.     rc = udebug_regs_read(app_phone, thread_hash, buffer);
  121.     cons_printf("udebug_regs_read -> %d\n", rc);
  122.     buffer[ISTATE_OFF_EFLAGS] &= ~0x0100; /* trap flag */
  123.     rc = udebug_regs_write(app_phone, thread_hash, buffer);
  124. }
  125.  
  126. /** @}
  127.  */
  128.