kernel/usermode/mpu_stack_objects.html · kernel/usermode/mpu_stack_objects.rst 공식 원문 ↗

Zephyr 3.7.0 · Kernel · Userspace

MPU Stack Objects

MPU region 제약을 linker stack 배치와 overflow guard에 반영해 invalid access를 hardware fault로 검출합니다.

Official pathkernel/usermode/mpu_stack_objects.html
Source filekernel/usermode/mpu_stack_objects.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

ARM v7-M power-of-two region과 K64F 32-byte boundary 방식, overlapping-region priority의 영향을 설명합니다.

Stack

contiguous allocation

Guard

lowest address · no-write

Placement

size · alignment · priority

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 879AA163E5594D0084F8785BB448E3C2B01359ABD20C6B3FA6D7B8A4DF99FFE3
.. _mpu_stack_objects:

MPU Stack Objects
#################

Thread Stack Creation
*********************

Thread stacks are declared statically with :c:macro:`K_THREAD_STACK_DEFINE()`.

For architectures which utilize memory protection unit (MPU) hardware,
stacks are physically contiguous allocations.  This contiguous allocation
has implications for the placement of stacks in memory, as well as the
implementation of other features such as stack protection and userspace.  The
implications for placement are directly attributed to the alignment
requirements for MPU regions.  This is discussed in the memory placement
section below.

Stack Guards
************

Stack protection mechanisms require hardware support that can restrict access
to memory.  Memory protection units can provide this kind of support.
The MPU provides a fixed number of regions.  Each region contains information
about the start, end, size, and access attributes to be enforced on that
particular region.

Stack guards are implemented by using a single MPU region and setting the
attributes for that region to not allow write access.  If invalid accesses
occur, a fault ensues.  The stack guard is defined at the bottom (the lowest
address) of the stack.

Memory Placement
****************

During stack creation, a set of constraints are enforced on the allocation of
memory.  These constraints include determining the alignment of the stack and
the correct sizing of the stack.  During linking of the binary, these
constraints are used to place the stacks properly.

The main source of the memory constraints is the MPU design for the SoC.  The
MPU design may require specific constraints on the region definition.  These
can include alignment of beginning and end addresses, sizes of allocations,
or even interactions between overlapping regions.

Some MPUs require that each region be aligned to a power of two.  These SoCs
will have :kconfig:option:`CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT` defined.
This means that a 1500 byte stack should be aligned to a 2kB boundary and the
stack size should also be adjusted to 2kB to ensure that nothing else is
placed in the remainder of the region.  SoCs which include the unmodified ARM
v7m MPU will have these constraints.

Some ARM MPUs use start and end addresses to define MPU regions and both the
start and end addresses require 32 byte alignment.  An example of this kind of
MPU is found in the NXP FRDM K64F.

MPUs may have a region priority mechanisms that use the highest priority region
that covers the memory access to determine the enforcement policy.  Others may
logically OR regions to determine enforcement policy.

Size and alignment constraints may result in stack allocations being larger
than the requested size.  Region priority mechanisms may result in
some added complexity when implementing stack guards.

Part 3

한국어 전문 번역

Thread stack 생성

Thread stack은 K_THREAD_STACK_DEFINE()으로 static 선언합니다. MPU architecture에서 stack은 physically contiguous allocation이며, 이 특성은 memory placement뿐 아니라 stack protection과 userspace 구현에도 영향을 줍니다. Placement 제약은 MPU region alignment requirement에서 직접 생깁니다.

Stack guards

Stack protection에는 memory access를 제한할 hardware가 필요하며 MPU가 이를 제공합니다. MPU는 fixed region 수를 갖고 각 region에 start, end, size와 enforced access attribute를 저장합니다.

Stack guard는 MPU region 하나를 write 불가로 설정해 구현합니다. Invalid access가 발생하면 fault가 나고 guard는 stack의 bottom, 즉 lowest address에 놓입니다.

MPU stack guard 배치
Guard region

lowest address · no-write · overflow fault

Thread stack

physically contiguous allocation

Top

stack 사용 방향에 따른 active end

Region 하나를 소비하며 MPU priority model에 따라 구현 복잡성이 달라집니다.

Memory placement

Stack 생성 시 alignment와 correct size constraint를 계산하고 linker가 이를 사용해 binary 안에 stack을 배치합니다. 주된 constraint는 SoC MPU design이며 start/end alignment, allocation size, overlapping region interaction이 포함될 수 있습니다.

Power-of-two alignment를 요구하는 MPU는 CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT를 정의합니다. 1500-byte stack도 2KB boundary에 align하고 size를 2KB로 올려 region 나머지에 다른 object가 놓이지 않게 합니다. Unmodified ARM v7-M MPU가 이 방식입니다.

일부 ARM MPU는 start·end address로 region을 정의하고 둘 다 32-byte alignment를 요구합니다. NXP FRDM K64F가 예입니다.

MPU access policy는 memory access를 덮는 highest-priority region을 선택하거나 여러 region policy를 logical OR할 수 있습니다. Size·alignment 때문에 실제 allocation이 요청보다 커질 수 있고 region priority 때문에 stack guard 구현도 복잡해질 수 있습니다.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.