#!/usr/bin/env python3 # # Copyright (c) 2017 Intel Corporation # # SPDX-License-Identifier: Apache-2.0 """Generate a Global Descriptor Table (GDT) for x86 CPUs. For additional detail on GDT and x86 memory management, please consult the IA Architecture SW Developer Manual, vol. 3. This script accepts as input the zephyr_prebuilt.elf binary, which is a link of the Zephyr kernel without various build-time generated data structures (such as the GDT) inserted into it. This kernel image has been properly padded such that inserting these data structures will not disturb the memory addresses of other symbols. The input kernel ELF binary is used to obtain the following information: - Memory addresses of the Main and Double Fault TSS structures so GDT descriptors can be created for them - Memory addresses of where the GDT lives in memory, so that this address can be populated in the GDT pseudo descriptor - whether userspace or HW stack protection are enabled in Kconfig The output is a GDT whose contents depend on the kernel configuration. With no memory protection features enabled, we generate flat 32-bit code and data segments. If hardware- based stack overflow protection or userspace is enabled, we additionally create descriptors for the main and double- fault IA tasks, needed for userspace privilege elevation and double-fault handling. If userspace is enabled, we also create flat code/data segments for ring 3 execution. """ import argparse import sys import struct import os from distutils.version import LooseVersion import elftools from elftools.elf.elffile import ELFFile from elftools.elf.sections import SymbolTableSection if LooseVersion(elftools.__version__) < LooseVersion('0.24'): sys.exit("pyelftools is out of date, need version 0.24 or later") def debug(text): """Display debug message if --verbose""" if args.verbose: sys.stdout.write(os.path.basename(sys.argv[0]) + ": " + text + "\n") def error(text): """Exit program with an error message""" sys.exit(os.path.basename(sys.argv[0]) + ": " + text) GDT_PD_FMT = "> 16) & 0xFF base_hi = (base >> 24) & 0xFF limit_lo = limit & 0xFFFF limit_hi = (limit >> 16) & 0xF return (base_lo, base_mid, base_hi, limit_lo, limit_hi) GDT_ENT_FMT = "= 5: main_tss = syms["_main_tss"] df_tss = syms["_df_tss"] # Selector 0x18: main TSS output_fp.write(create_tss_entry(main_tss, 0x67, 0)) # Selector 0x20: double-fault TSS output_fp.write(create_tss_entry(df_tss, 0x67, 0)) if num_entries >= 7: # Selector 0x28: code descriptor, dpl = 3 output_fp.write(create_code_data_entry(0, 0xFFFFF, 3, FLAGS_GRAN, ACCESS_EX | ACCESS_RW)) # Selector 0x30: data descriptor, dpl = 3 output_fp.write(create_code_data_entry(0, 0xFFFFF, 3, FLAGS_GRAN, ACCESS_RW)) if use_tls: # Selector 0x18, 0x28 or 0x38 (depending on entries above): # data descriptor, dpl = 3 # # for use with thread local storage while this will be # modified at runtime. output_fp.write(create_code_data_entry(0, 0xFFFFF, 3, FLAGS_GRAN, ACCESS_RW)) if __name__ == "__main__": main()