Subversion Repositories HelenOS

Rev

Rev 2935 | 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. #include <kernel/arch/context_offset.h>
  40.  
  41. #include "../../../main.h"
  42. #include "../../../include/arch.h"
  43.  
  44. #define OPCODE_BREAK        0x0000000d
  45.  
  46. void arch_breakpoint_add(uintptr_t addr)
  47. {
  48.     uint32_t brkp;
  49.     int rc;
  50.     breakpoint_t *brk;
  51.     int i;
  52.  
  53.     brk = NULL;
  54.     for (i = 0; i < MAX_BRKPTS; i++)
  55.         if (brk_list[i].set == 0) {
  56.             brk = brk_list+i;
  57.             break;
  58.         }
  59.  
  60.     if (!brk) {
  61.         printf("too many breakpoints\n");
  62.         return;
  63.     }
  64.  
  65.     rc = udebug_mem_read(app_phone, &brk->arch.back, addr, sizeof(&brk->arch.back));
  66.     printf("udebug_mem_read() -> %d\n", rc);
  67.     brkp = OPCODE_BREAK;
  68.     rc = udebug_mem_write(app_phone, &brkp, addr, sizeof(brkp));
  69. //  for (i=0; i<256; i++) rc = udebug_mem_write(app_phone, &brkp, addr+4*i, sizeof(brkp));
  70.     printf("udebug_mem_write() -> %d\n", rc);
  71.  
  72.     brk->addr = addr;
  73.     brk->set = 1;
  74. }
  75.  
  76. static unsigned buffer[1024];
  77. static breakpoint_t *lifted_brkpt;
  78.  
  79. void arch_event_breakpoint(thash_t thread_hash)
  80. {
  81.     int rc;
  82.     uint32_t epc;
  83.     int brk_addr;
  84.     uint32_t brkp;
  85.  
  86.     brkp = OPCODE_BREAK;
  87.  
  88.     rc = udebug_regs_read(app_phone, thread_hash, buffer);
  89.     printf("udebug_regs_read -> %d\n", rc);
  90.     epc = buffer[EOFFSET_EPC/sizeof(unsigned)];
  91.     printf("EPC was 0x%08x\n", epc);
  92.     brk_addr = epc;
  93.  
  94.     int bi;
  95.     for (bi = 0; bi < MAX_BRKPTS; bi++) {
  96.         if (brk_list[bi].set && brk_list[bi].addr == brk_addr)
  97.             break;
  98.     }
  99.     if (bi < MAX_BRKPTS) {
  100.         printf("breakpoint %d hit\n", bi);
  101.             rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 4);
  102.         printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc);
  103.         rc = udebug_mem_read(app_phone, &brk_list[bi].arch.back, brk_addr+4, 4);
  104.             rc = udebug_mem_write(app_phone, &brkp, brk_addr+4, 4);
  105.         lifted_brkpt = &brk_list[bi];
  106.         return;
  107.     }
  108.  
  109.     for (bi = 0; bi < MAX_BRKPTS; bi++) {
  110.         if (brk_list[bi].set && brk_list[bi].addr + 4 == brk_addr)
  111.             break;
  112.     }
  113.     if (bi < MAX_BRKPTS) {
  114.         printf("restoring breakpoint %d\n", bi);
  115.             rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 4);
  116.         rc = udebug_mem_read(app_phone, &brk_list[bi].arch.back, brk_addr-4, 4);
  117.             rc = udebug_mem_write(app_phone, &brkp, brk_addr-4, 4);
  118.         lifted_brkpt = NULL;
  119.         return;
  120.     }
  121.  
  122.     printf("unrecognized breakpoint at 0x%x\n", brk_addr);
  123. }
  124.  
  125. void arch_event_trap(thash_t thread_hash)
  126. {
  127.     /* Unused */
  128.     (void)thread_hash;
  129. }
  130.  
  131. /** @}
  132.  */
  133.