Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2017 Linaro Limited |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | |
Andrew Boie | c78c5e6 | 2019-03-11 14:45:43 -0700 | [diff] [blame] | 7 | """ |
| 8 | Script to generate gperf tables mapping threads to their privileged mode stacks |
| 9 | |
| 10 | Some MPU devices require that memory region definitions be aligned to their |
| 11 | own size, which must be a power of two. This introduces difficulties in |
| 12 | reserving memory for the thread's supervisor mode stack inline with the |
| 13 | K_THREAD_STACK_DEFINE() macro. |
| 14 | |
| 15 | Instead, the stack used when a user thread elevates privileges is allocated |
| 16 | elsewhere in memory, and a gperf table is created to be able to quickly |
| 17 | determine where the supervisor mode stack is in memory. This is accomplished |
| 18 | by scanning the DWARF debug information in zephyr_prebuilt.elf, identifying |
| 19 | instances of 'struct k_thread', and emitting a gperf configuration file which |
| 20 | allocates memory for each thread's privileged stack and creates the table |
| 21 | mapping thread addresses to these stacks. |
| 22 | """ |
| 23 | |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 24 | import sys |
| 25 | import argparse |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 26 | import struct |
Andy Gross | 6042ae9 | 2018-01-22 14:26:49 -0600 | [diff] [blame] | 27 | from elf_helper import ElfHelper |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 28 | |
Andrew Boie | c235e16 | 2019-03-27 14:27:24 -0700 | [diff] [blame] | 29 | kobjects = { |
| 30 | "_k_thread_stack_element": (None, False) |
| 31 | } |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 32 | |
| 33 | |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 34 | header = """%compare-lengths |
Patrik Flykt | 7c0a245 | 2019-03-14 09:20:46 -0600 | [diff] [blame] | 35 | %define lookup-function-name z_priv_stack_map_lookup |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 36 | %language=ANSI-C |
| 37 | %global-table |
| 38 | %struct-type |
| 39 | """ |
| 40 | |
| 41 | |
Ioannis Glaropoulos | 88959e7 | 2019-06-13 18:11:58 +0200 | [diff] [blame] | 42 | # Each privilege stack buffer needs to respect the alignment |
| 43 | # constraints as specified in arm/arch.h. |
Andy Gross | 6042ae9 | 2018-01-22 14:26:49 -0600 | [diff] [blame] | 44 | priv_stack_decl_temp = ("static u8_t __used" |
Ioannis Glaropoulos | 88959e7 | 2019-06-13 18:11:58 +0200 | [diff] [blame] | 45 | " __aligned(Z_PRIVILEGE_STACK_ALIGN)" |
Andy Gross | 6042ae9 | 2018-01-22 14:26:49 -0600 | [diff] [blame] | 46 | " priv_stack_%x[CONFIG_PRIVILEGED_STACK_SIZE];\n") |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 47 | |
| 48 | |
| 49 | includes = """#include <kernel.h> |
| 50 | #include <string.h> |
| 51 | """ |
| 52 | |
| 53 | |
| 54 | structure = """struct _k_priv_stack_map { |
| 55 | char *name; |
| 56 | u8_t *priv_stack_addr; |
| 57 | }; |
| 58 | %% |
| 59 | """ |
| 60 | |
| 61 | |
Andy Gross | 6042ae9 | 2018-01-22 14:26:49 -0600 | [diff] [blame] | 62 | # Different versions of gperf have different prototypes for the lookup |
| 63 | # function, best to implement the wrapper here. The pointer value itself is |
| 64 | # turned into a string, we told gperf to expect binary strings that are not |
| 65 | # NULL-terminated. |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 66 | footer = """%% |
Patrik Flykt | 7c0a245 | 2019-03-14 09:20:46 -0600 | [diff] [blame] | 67 | u8_t *z_priv_stack_find(void *obj) |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 68 | { |
| 69 | const struct _k_priv_stack_map *map = |
Patrik Flykt | 7c0a245 | 2019-03-14 09:20:46 -0600 | [diff] [blame] | 70 | z_priv_stack_map_lookup((const char *)obj, sizeof(void *)); |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 71 | return map->priv_stack_addr; |
| 72 | } |
| 73 | """ |
| 74 | |
| 75 | |
Ulf Magnusson | 1a27de0 | 2019-03-25 20:02:21 +0100 | [diff] [blame] | 76 | def write_gperf_table(fp, eh, objs): |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 77 | fp.write(header) |
| 78 | |
| 79 | # priv stack declarations |
| 80 | fp.write("%{\n") |
| 81 | fp.write(includes) |
Ulf Magnusson | 12ba9df | 2019-03-19 19:28:24 +0100 | [diff] [blame] | 82 | for obj_addr in objs: |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 83 | fp.write(priv_stack_decl_temp % (obj_addr)) |
| 84 | fp.write("%}\n") |
| 85 | |
| 86 | # structure declaration |
| 87 | fp.write(structure) |
| 88 | |
Ulf Magnusson | 12ba9df | 2019-03-19 19:28:24 +0100 | [diff] [blame] | 89 | for obj_addr in objs: |
Andy Gross | 6042ae9 | 2018-01-22 14:26:49 -0600 | [diff] [blame] | 90 | byte_str = struct.pack("<I" if eh.little_endian else ">I", obj_addr) |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 91 | fp.write("\"") |
| 92 | for byte in byte_str: |
| 93 | val = "\\x%02x" % byte |
| 94 | fp.write(val) |
| 95 | |
| 96 | fp.write("\",priv_stack_%x\n" % obj_addr) |
| 97 | |
| 98 | fp.write(footer) |
| 99 | |
| 100 | |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 101 | def parse_args(): |
| 102 | global args |
| 103 | |
Andy Gross | 6042ae9 | 2018-01-22 14:26:49 -0600 | [diff] [blame] | 104 | parser = argparse.ArgumentParser( |
| 105 | description=__doc__, |
| 106 | formatter_class=argparse.RawDescriptionHelpFormatter) |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 107 | |
| 108 | parser.add_argument("-k", "--kernel", required=True, |
Andy Gross | 6042ae9 | 2018-01-22 14:26:49 -0600 | [diff] [blame] | 109 | help="Input zephyr ELF binary") |
| 110 | parser.add_argument( |
| 111 | "-o", "--output", required=True, |
| 112 | help="Output list of kernel object addresses for gperf use") |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 113 | parser.add_argument("-v", "--verbose", action="store_true", |
Andy Gross | 6042ae9 | 2018-01-22 14:26:49 -0600 | [diff] [blame] | 114 | help="Print extra debugging information") |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 115 | args = parser.parse_args() |
| 116 | |
| 117 | |
| 118 | def main(): |
| 119 | parse_args() |
| 120 | |
Andy Gross | 6042ae9 | 2018-01-22 14:26:49 -0600 | [diff] [blame] | 121 | eh = ElfHelper(args.kernel, args.verbose, kobjects, []) |
| 122 | syms = eh.get_symbols() |
| 123 | max_threads = syms["CONFIG_MAX_THREAD_BYTES"] * 8 |
| 124 | objs = eh.find_kobjects(syms) |
Marc Herbert | f78288b | 2019-03-05 14:31:44 -0800 | [diff] [blame] | 125 | if not objs: |
| 126 | sys.stderr.write("WARNING: zero kobject found in %s\n" |
| 127 | % args.kernel) |
Andy Gross | 6042ae9 | 2018-01-22 14:26:49 -0600 | [diff] [blame] | 128 | |
Anas Nashif | 2e7bdb6 | 2018-09-16 14:56:07 -0500 | [diff] [blame] | 129 | thread_counter = eh.get_thread_counter() |
| 130 | if thread_counter > max_threads: |
Andy Gross | 6042ae9 | 2018-01-22 14:26:49 -0600 | [diff] [blame] | 131 | sys.stderr.write("Too many thread objects (%d)\n" % thread_counter) |
| 132 | sys.stderr.write("Increase CONFIG_MAX_THREAD_BYTES to %d\n", |
| 133 | -(-thread_counter // 8)) |
| 134 | sys.exit(1) |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 135 | |
| 136 | with open(args.output, "w") as fp: |
Ulf Magnusson | 1a27de0 | 2019-03-25 20:02:21 +0100 | [diff] [blame] | 137 | write_gperf_table(fp, eh, objs) |
Andy Gross | 6042ae9 | 2018-01-22 14:26:49 -0600 | [diff] [blame] | 138 | |
Chunlin Han | 18560a0 | 2018-02-01 01:19:49 -0600 | [diff] [blame] | 139 | |
| 140 | if __name__ == "__main__": |
| 141 | main() |