qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

leon3: add a little bootloader

This adds a little bootloader to the leon3_machine when a ram image is
given through the kernel parameter and no bios are provided:
* The UART transmiter is enabled.
* The TIMER is initialized.

Reviewed-by: Fabien Chouteau <chouteau@adacore.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: KONRAD Frederic <frederic.konrad@adacore.com>
Signed-off-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>

authored by

KONRAD Frederic and committed by
Mark Cave-Ayland
dbed0d2d b70447aa

+71 -8
+71 -8
hw/sparc/leon3.c
··· 44 44 #define CPU_CLK (40 * 1000 * 1000) 45 45 46 46 #define LEON3_PROM_FILENAME "u-boot.bin" 47 + #define LEON3_PROM_OFFSET (0x00000000) 48 + #define LEON3_RAM_OFFSET (0x40000000) 47 49 48 50 #define MAX_PILS 16 49 51 ··· 62 64 target_ulong sp; /* initial stack pointer */ 63 65 } ResetData; 64 66 67 + static uint32_t *gen_store_u32(uint32_t *code, hwaddr addr, uint32_t val) 68 + { 69 + stl_p(code++, 0x82100000); /* mov %g0, %g1 */ 70 + stl_p(code++, 0x84100000); /* mov %g0, %g2 */ 71 + stl_p(code++, 0x03000000 + 72 + extract32(addr, 10, 22)); 73 + /* sethi %hi(addr), %g1 */ 74 + stl_p(code++, 0x82106000 + 75 + extract32(addr, 0, 10)); 76 + /* or %g1, addr, %g1 */ 77 + stl_p(code++, 0x05000000 + 78 + extract32(val, 10, 22)); 79 + /* sethi %hi(val), %g2 */ 80 + stl_p(code++, 0x8410a000 + 81 + extract32(val, 0, 10)); 82 + /* or %g2, val, %g2 */ 83 + stl_p(code++, 0xc4204000); /* st %g2, [ %g1 ] */ 84 + 85 + return code; 86 + } 87 + 88 + /* 89 + * When loading a kernel in RAM the machine is expected to be in a different 90 + * state (eg: initialized by the bootloader). This little code reproduces 91 + * this behavior. 92 + */ 93 + static void write_bootloader(CPUSPARCState *env, uint8_t *base, 94 + hwaddr kernel_addr) 95 + { 96 + uint32_t *p = (uint32_t *) base; 97 + 98 + /* Initialize the UARTs */ 99 + /* *UART_CONTROL = UART_RECEIVE_ENABLE | UART_TRANSMIT_ENABLE; */ 100 + p = gen_store_u32(p, 0x80000108, 3); 101 + 102 + /* Initialize the TIMER 0 */ 103 + /* *GPTIMER_SCALER_RELOAD = 40 - 1; */ 104 + p = gen_store_u32(p, 0x80000304, 39); 105 + /* *GPTIMER0_COUNTER_RELOAD = 0xFFFE; */ 106 + p = gen_store_u32(p, 0x80000314, 0xFFFFFFFE); 107 + /* *GPTIMER0_CONFIG = GPTIMER_ENABLE | GPTIMER_RESTART; */ 108 + p = gen_store_u32(p, 0x80000318, 3); 109 + 110 + /* JUMP to the entry point */ 111 + stl_p(p++, 0x82100000); /* mov %g0, %g1 */ 112 + stl_p(p++, 0x03000000 + extract32(kernel_addr, 10, 22)); 113 + /* sethi %hi(kernel_addr), %g1 */ 114 + stl_p(p++, 0x82106000 + extract32(kernel_addr, 0, 10)); 115 + /* or kernel_addr, %g1 */ 116 + stl_p(p++, 0x81c04000); /* jmp %g1 */ 117 + stl_p(p++, 0x01000000); /* nop */ 118 + } 119 + 65 120 static void main_cpu_reset(void *opaque) 66 121 { 67 122 ResetData *s = (ResetData *)opaque; ··· 142 197 /* Reset data */ 143 198 reset_info = g_malloc0(sizeof(ResetData)); 144 199 reset_info->cpu = cpu; 145 - reset_info->sp = 0x40000000 + ram_size; 200 + reset_info->sp = LEON3_RAM_OFFSET + ram_size; 146 201 qemu_register_reset(main_cpu_reset, reset_info); 147 202 148 203 /* Allocate IRQ manager */ ··· 164 219 } 165 220 166 221 memory_region_allocate_system_memory(ram, NULL, "leon3.ram", ram_size); 167 - memory_region_add_subregion(address_space_mem, 0x40000000, ram); 222 + memory_region_add_subregion(address_space_mem, LEON3_RAM_OFFSET, ram); 168 223 169 224 /* Allocate BIOS */ 170 225 prom_size = 8 * MiB; 171 226 memory_region_init_ram(prom, NULL, "Leon3.bios", prom_size, &error_fatal); 172 227 memory_region_set_readonly(prom, true); 173 - memory_region_add_subregion(address_space_mem, 0x00000000, prom); 228 + memory_region_add_subregion(address_space_mem, LEON3_PROM_OFFSET, prom); 174 229 175 230 /* Load boot prom */ 176 231 if (bios_name == NULL) { ··· 190 245 } 191 246 192 247 if (bios_size > 0) { 193 - ret = load_image_targphys(filename, 0x00000000, bios_size); 248 + ret = load_image_targphys(filename, LEON3_PROM_OFFSET, bios_size); 194 249 if (ret < 0 || ret > prom_size) { 195 250 error_report("could not load prom '%s'", filename); 196 251 exit(1); ··· 220 275 exit(1); 221 276 } 222 277 if (bios_size <= 0) { 223 - /* If there is no bios/monitor, start the application. */ 224 - env->pc = entry; 225 - env->npc = entry + 4; 226 - reset_info->entry = entry; 278 + /* 279 + * If there is no bios/monitor just start the application but put 280 + * the machine in an initialized state through a little 281 + * bootloader. 282 + */ 283 + uint8_t *bootloader_entry; 284 + 285 + bootloader_entry = memory_region_get_ram_ptr(prom); 286 + write_bootloader(env, bootloader_entry, entry); 287 + env->pc = LEON3_PROM_OFFSET; 288 + env->npc = LEON3_PROM_OFFSET + 4; 289 + reset_info->entry = LEON3_PROM_OFFSET; 227 290 } 228 291 } 229 292