2018-02-01 15:19:49 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright (c) 2017 Linaro Limited
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2019-03-12 05:45:43 +08:00
|
|
|
"""
|
|
|
|
Script to generate gperf tables mapping threads to their privileged mode stacks
|
|
|
|
|
|
|
|
Some MPU devices require that memory region definitions be aligned to their
|
|
|
|
own size, which must be a power of two. This introduces difficulties in
|
|
|
|
reserving memory for the thread's supervisor mode stack inline with the
|
|
|
|
K_THREAD_STACK_DEFINE() macro.
|
|
|
|
|
|
|
|
Instead, the stack used when a user thread elevates privileges is allocated
|
|
|
|
elsewhere in memory, and a gperf table is created to be able to quickly
|
|
|
|
determine where the supervisor mode stack is in memory. This is accomplished
|
|
|
|
by scanning the DWARF debug information in zephyr_prebuilt.elf, identifying
|
|
|
|
instances of 'struct k_thread', and emitting a gperf configuration file which
|
|
|
|
allocates memory for each thread's privileged stack and creates the table
|
|
|
|
mapping thread addresses to these stacks.
|
|
|
|
"""
|
|
|
|
|
2018-02-01 15:19:49 +08:00
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import struct
|
2018-01-23 04:26:49 +08:00
|
|
|
from elf_helper import ElfHelper
|
2018-02-01 15:19:49 +08:00
|
|
|
|
2019-03-28 05:27:24 +08:00
|
|
|
kobjects = {
|
|
|
|
"_k_thread_stack_element": (None, False)
|
|
|
|
}
|
2018-02-01 15:19:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
header = """%compare-lengths
|
2019-03-14 23:20:46 +08:00
|
|
|
%define lookup-function-name z_priv_stack_map_lookup
|
2018-02-01 15:19:49 +08:00
|
|
|
%language=ANSI-C
|
|
|
|
%global-table
|
|
|
|
%struct-type
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-06-14 00:11:58 +08:00
|
|
|
# Each privilege stack buffer needs to respect the alignment
|
|
|
|
# constraints as specified in arm/arch.h.
|
2018-01-23 04:26:49 +08:00
|
|
|
priv_stack_decl_temp = ("static u8_t __used"
|
2019-06-14 00:11:58 +08:00
|
|
|
" __aligned(Z_PRIVILEGE_STACK_ALIGN)"
|
2018-01-23 04:26:49 +08:00
|
|
|
" priv_stack_%x[CONFIG_PRIVILEGED_STACK_SIZE];\n")
|
2018-02-01 15:19:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
includes = """#include <kernel.h>
|
|
|
|
#include <string.h>
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
structure = """struct _k_priv_stack_map {
|
|
|
|
char *name;
|
|
|
|
u8_t *priv_stack_addr;
|
|
|
|
};
|
|
|
|
%%
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-01-23 04:26:49 +08:00
|
|
|
# Different versions of gperf have different prototypes for the lookup
|
|
|
|
# function, best to implement the wrapper here. The pointer value itself is
|
|
|
|
# turned into a string, we told gperf to expect binary strings that are not
|
|
|
|
# NULL-terminated.
|
2018-02-01 15:19:49 +08:00
|
|
|
footer = """%%
|
2019-03-14 23:20:46 +08:00
|
|
|
u8_t *z_priv_stack_find(void *obj)
|
2018-02-01 15:19:49 +08:00
|
|
|
{
|
|
|
|
const struct _k_priv_stack_map *map =
|
2019-03-14 23:20:46 +08:00
|
|
|
z_priv_stack_map_lookup((const char *)obj, sizeof(void *));
|
2018-02-01 15:19:49 +08:00
|
|
|
return map->priv_stack_addr;
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-03-26 03:02:21 +08:00
|
|
|
def write_gperf_table(fp, eh, objs):
|
2018-02-01 15:19:49 +08:00
|
|
|
fp.write(header)
|
|
|
|
|
|
|
|
# priv stack declarations
|
|
|
|
fp.write("%{\n")
|
|
|
|
fp.write(includes)
|
2019-03-20 02:28:24 +08:00
|
|
|
for obj_addr in objs:
|
2018-02-01 15:19:49 +08:00
|
|
|
fp.write(priv_stack_decl_temp % (obj_addr))
|
|
|
|
fp.write("%}\n")
|
|
|
|
|
|
|
|
# structure declaration
|
|
|
|
fp.write(structure)
|
|
|
|
|
2019-03-20 02:28:24 +08:00
|
|
|
for obj_addr in objs:
|
2018-01-23 04:26:49 +08:00
|
|
|
byte_str = struct.pack("<I" if eh.little_endian else ">I", obj_addr)
|
2018-02-01 15:19:49 +08:00
|
|
|
fp.write("\"")
|
|
|
|
for byte in byte_str:
|
|
|
|
val = "\\x%02x" % byte
|
|
|
|
fp.write(val)
|
|
|
|
|
|
|
|
fp.write("\",priv_stack_%x\n" % obj_addr)
|
|
|
|
|
|
|
|
fp.write(footer)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
global args
|
|
|
|
|
2018-01-23 04:26:49 +08:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description=__doc__,
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
2018-02-01 15:19:49 +08:00
|
|
|
|
|
|
|
parser.add_argument("-k", "--kernel", required=True,
|
2018-01-23 04:26:49 +08:00
|
|
|
help="Input zephyr ELF binary")
|
|
|
|
parser.add_argument(
|
|
|
|
"-o", "--output", required=True,
|
|
|
|
help="Output list of kernel object addresses for gperf use")
|
2018-02-01 15:19:49 +08:00
|
|
|
parser.add_argument("-v", "--verbose", action="store_true",
|
2018-01-23 04:26:49 +08:00
|
|
|
help="Print extra debugging information")
|
2018-02-01 15:19:49 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parse_args()
|
|
|
|
|
2018-01-23 04:26:49 +08:00
|
|
|
eh = ElfHelper(args.kernel, args.verbose, kobjects, [])
|
|
|
|
syms = eh.get_symbols()
|
|
|
|
max_threads = syms["CONFIG_MAX_THREAD_BYTES"] * 8
|
|
|
|
objs = eh.find_kobjects(syms)
|
2019-03-06 06:31:44 +08:00
|
|
|
if not objs:
|
|
|
|
sys.stderr.write("WARNING: zero kobject found in %s\n"
|
|
|
|
% args.kernel)
|
2018-01-23 04:26:49 +08:00
|
|
|
|
2018-09-17 03:56:07 +08:00
|
|
|
thread_counter = eh.get_thread_counter()
|
|
|
|
if thread_counter > max_threads:
|
2019-09-07 20:41:01 +08:00
|
|
|
sys.exit("Too many thread objects ({})\n"
|
|
|
|
"Increase CONFIG_MAX_THREAD_BYTES to {}"
|
|
|
|
.format(thread_counter, -(-thread_counter // 8)))
|
2018-02-01 15:19:49 +08:00
|
|
|
|
|
|
|
with open(args.output, "w") as fp:
|
2019-03-26 03:02:21 +08:00
|
|
|
write_gperf_table(fp, eh, objs)
|
2018-01-23 04:26:49 +08:00
|
|
|
|
2018-02-01 15:19:49 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|