YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
double_init_rom.c
1/*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <string.h>
8#include <assert.h>
9#include "bootrom.h"
10#include "sf_table.h"
11
12void __attribute((noreturn)) panic(char *);
13uint8_t rp2040_rom_version(void);
14
15// NOTE THIS FUNCTION TABLE IS NOT PUBLIC OR NECESSARILY COMPLETE...
16// IT IS ***NOT*** SAFE TO CALL THESE FUNCTION POINTERS FROM ARBITRARY CODE
17uint32_t sd_table[SF_TABLE_V2_SIZE / 2];
18
19#if !(PICO_DOUBLE_SUPPORT_ROM_V1 && PICO_RP2040_B0_SUPPORTED)
20static __attribute__((noreturn)) void missing_double_func_shim(void) {
21 panic("missing double function");
22}
23#endif
24extern void double_table_shim_on_use_helper(void);
25
26void __attribute__((weak)) *sf_clz_func;
27
28void __aeabi_double_init(void) {
29 int rom_version = rp2040_rom_version();
30#if PICO_DOUBLE_SUPPORT_ROM_V1 && PICO_RP2040_B0_SUPPORTED
31 if (rom_version == 1) {
32
33 // this is a little tricky.. we only want to pull in a shim if the corresponding function
34 // is called. to that end we include a SVC instruction with the table offset as the call number
35 // followed by the shim function pointer inside the actual wrapper function. that way if the wrapper
36 // function is garbage collected, so is the shim function.
37 //
38 // double_table_shim_on_use_helper expects this SVC instruction in the calling code soon after the address
39 // pointed to by IP and patches the double_table entry with the real shim the first time the function is called.
40 for(uint32_t i=0; i<SF_TABLE_V2_SIZE/4; i++) {
41 sd_table[i] = (uintptr_t)double_table_shim_on_use_helper;
42 }
43 }
44#else
45 if (rom_version == 1) {
46 // opting for soft failure for now - you'll get a panic at runtime if you call any of the missing methods
47 for(uint32_t i=0;i<SF_TABLE_V2_SIZE/4;i++) {
48 sd_table[i] = (uintptr_t)missing_double_func_shim;
49 }
50 }
51#endif
52 if (rom_version >= 2) {
53 void *rom_table = rom_data_lookup(rom_table_code('S', 'D'));
54 assert(*((uint8_t *)(((void *)rom_data_lookup(rom_table_code('S', 'F')))-2)) * 4 >= SF_TABLE_V2_SIZE);
55 memcpy(&sd_table, rom_table, SF_TABLE_V2_SIZE);
56 if (rom_version == 2) {
57#ifndef NDEBUG
58 if (*(uint16_t *)0x3854 != 0xb500 || // this is dsincos(_internal)
59
60 *(uint16_t *)0x38d8 != 0x4649 || // this is dsin_finish
61 *(uint16_t *)0x389c != 0x4659 // this is dcos_finish
62 ) {
63 panic(NULL);
64 }
65#endif
66 }
67 }
68 if (rom_version < 3) {
69 // we use the unused entry for SINCOS
70 sd_table[SF_TABLE_V3_FSINCOS / 4] = (uintptr_t) double_table_shim_on_use_helper;
71 }
72
73 sf_clz_func = rom_func_lookup(ROM_FUNC_CLZ32);
74}
void * rom_func_lookup(uint32_t code)
Lookup a bootrom function by code.
Definition bootrom.c:15
void * rom_data_lookup(uint32_t code)
Lookup a bootrom address by code.
Definition bootrom.c:19
void __attribute__((noreturn))(*rom_reset_usb_boot_fn)(uint32_t
Reboot the device into BOOTSEL mode.
Definition bootrom.h:66