1.. _architecture_porting_guide:
2
3Architecture Porting Guide
4##########################
5
6An architecture port is needed to enable Zephyr to run on an :abbr:`ISA
7(instruction set architecture)` or an :abbr:`ABI (Application Binary
8Interface)` that is not currently supported.
9
10The following are examples of ISAs and ABIs that Zephyr supports:
11
12* x86_32 ISA with System V ABI
13* ARMv7-M ISA with Thumb2 instruction set and ARM Embedded ABI (aeabi)
14* ARCv2 ISA
15
16For information on Kconfig configuration, see
17:ref:`setting_configuration_values`. Architectures use a Kconfig configuration
18scheme similar to boards.
19
20An architecture port can be divided in several parts; most are required and
21some are optional:
22
23* **The early boot sequence**: each architecture has different steps it must
24  take when the CPU comes out of reset (required).
25
26* **Interrupt and exception handling**: each architecture handles asynchronous
27  and unrequested events in a specific manner (required).
28
29* **Thread context switching**: the Zephyr context switch is dependent on the
30  ABI and each ISA has a different set of registers to save (required).
31
32* **Thread creation and termination**: A thread's initial stack frame is ABI
33  and architecture-dependent, and thread abortion possibly as well (required).
34
35* **Device drivers**: most often, the system clock timer and the interrupt
36  controller are tied to the architecture (some required, some optional).
37
38* **Utility libraries**: some common kernel APIs rely on a
39  architecture-specific implementation for performance reasons (required).
40
41* **CPU idling/power management**: most architectures implement instructions
42  for putting the CPU to sleep (partly optional, most likely very desired).
43
44* **Fault management**: for implementing architecture-specific debug help and
45  handling of fatal error in threads (partly optional).
46
47* **Linker scripts and toolchains**: architecture-specific details will most
48  likely be needed in the build system and when linking the image (required).
49
50* **Memory Management and Memory Mapping**: for architecture-specific details
51  on supporting memory management and memory mapping.
52
53* **Stack Objects**: for architecture-specific details on memory protection
54  hardware regarding stack objects.
55
56* **User Mode Threads**: for supporting threads in user mode.
57
58* **GDB Stub**: for supporting GDB stub to enable remote debugging.
59
60Early Boot Sequence
61*******************
62
63The goal of the early boot sequence is to take the system from the state it is
64after reset to a state where is can run C code and thus the common kernel
65initialization sequence. Most of the time, very few steps are needed, while
66some architectures require a bit more work to be performed.
67
68Common steps for all architectures:
69
70* Setup an initial stack.
71* If running an :abbr:`XIP (eXecute-In-Place)` kernel, copy initialized data
72  from ROM to RAM.
73* If not using an ELF loader, zero the BSS section.
74* Jump to :code:`z_cstart()`, the early kernel initialization
75
76  * :code:`z_cstart()` is responsible for context switching out of the fake
77    context running at startup into the main thread.
78
79Some examples of architecture-specific steps that have to be taken:
80
81* If given control in real mode on x86_32, switch to 32-bit protected mode.
82* Setup the segment registers on x86_32 to handle boot loaders that leave them
83  in an unknown or broken state.
84* Initialize a board-specific watchdog on Cortex-M3/4.
85* Switch stacks from MSP to PSP on Cortex-M.
86* Use a different approach than calling into _Swap() on Cortex-M to prevent
87  race conditions.
88* Setup FIRQ and regular IRQ handling on ARCv2.
89
90Interrupt and Exception Handling
91********************************
92
93Each architecture defines interrupt and exception handling differently.
94
95When a device wants to signal the processor that there is some work to be done
96on its behalf, it raises an interrupt. When a thread does an operation that is
97not handled by the serial flow of the software itself, it raises an exception.
98Both, interrupts and exceptions, pass control to a handler. The handler is
99known as an :abbr:`ISR (Interrupt Service Routine)` in the case of
100interrupts. The handler performs the work required by the exception or the
101interrupt.  For interrupts, that work is device-specific. For exceptions, it
102depends on the exception, but most often the core kernel itself is responsible
103for providing the handler.
104
105The kernel has to perform some work in addition to the work the handler itself
106performs. For example:
107
108* Prior to handing control to the handler:
109
110  * Save the currently executing context.
111  * Possibly getting out of power saving mode, which includes waking up
112    devices.
113  * Updating the kernel uptime if getting out of tickless idle mode.
114
115* After getting control back from the handler:
116
117  * Decide whether to perform a context switch.
118  * When performing a context switch, restore the context being context
119    switched in.
120
121This work is conceptually the same across architectures, but the details are
122completely different:
123
124* The registers to save and restore.
125* The processor instructions to perform the work.
126* The numbering of the exceptions.
127* etc.
128
129It thus needs an architecture-specific implementation, called the
130interrupt/exception stub.
131
132Another issue is that the kernel defines the signature of ISRs as:
133
134.. code-block:: C
135
136    void (*isr)(void *parameter)
137
138Architectures do not have a consistent or native way of handling parameters to
139an ISR. As such there are two commonly used methods for handling the
140parameter.
141
142* Using some architecture defined mechanism, the parameter value is forced in
143  the stub. This is commonly found in X86-based architectures.
144
145* The parameters to the ISR are inserted and tracked via a separate table
146  requiring the architecture to discover at runtime which interrupt is
147  executing. A common interrupt handler demuxer is installed for all entries of
148  the real interrupt vector table, which then fetches the device's ISR and
149  parameter from the separate table. This approach is commonly used in the ARC
150  and ARM architectures via the :kconfig:option:`CONFIG_GEN_ISR_TABLES` implementation.
151  You can find examples of the stubs by looking at :code:`_interrupt_enter()` in
152  x86, :code:`_IntExit()` in ARM, :code:`_isr_wrapper()` in ARM, or the full
153  implementation description for ARC in :zephyr_file:`arch/arc/core/isr_wrapper.S`.
154
155Each architecture also has to implement primitives for interrupt control:
156
157* locking interrupts: :c:macro:`irq_lock()`, :c:macro:`irq_unlock()`.
158* registering interrupts: :c:macro:`IRQ_CONNECT()`.
159* programming the priority if possible :c:func:`irq_priority_set`.
160* enabling/disabling interrupts: :c:macro:`irq_enable()`, :c:macro:`irq_disable()`.
161
162.. note::
163
164  :c:macro:`IRQ_CONNECT` is a macro that uses assembler and/or linker script
165  tricks to connect interrupts at build time, saving boot time and text size.
166
167The vector table should contain a handler for each interrupt and exception that
168can possibly occur. The handler can be as simple as a spinning loop. However,
169we strongly suggest that handlers at least print some debug information. The
170information helps figuring out what went wrong when hitting an exception that
171is a fault, like divide-by-zero or invalid memory access, or an interrupt that
172is not expected (:dfn:`spurious interrupt`). See the ARM implementation in
173:zephyr_file:`arch/arm/core/cortex_m/fault.c` for an example.
174
175Thread Context Switching
176************************
177
178Multi-threading is the basic purpose to have a kernel at all. Zephyr supports
179two types of threads: preemptible and cooperative.
180
181Two crucial concepts when writing an architecture port are the following:
182
183* Cooperative threads run at a higher priority than preemptible ones, and
184  always preempt them.
185
186* After handling an interrupt, if a cooperative thread was interrupted, the
187  kernel always goes back to running that thread, since it is not preemptible.
188
189A context switch can happen in several circumstances:
190
191* When a thread executes a blocking operation, such as taking a semaphore that
192  is currently unavailable.
193
194* When a preemptible thread unblocks a thread of higher priority by releasing
195  the object on which it was blocked.
196
197* When an interrupt unblocks a thread of higher priority than the one currently
198  executing, if the currently executing thread is preemptible.
199
200* When a thread runs to completion.
201
202* When a thread causes a fatal exception and is removed from the running
203  threads. For example, referencing invalid memory,
204
205Therefore, the context switching must thus be able to handle all these cases.
206
207The kernel keeps the next thread to run in a "cache", and thus the context
208switching code only has to fetch from that cache to select which thread to run.
209
210There are two types of context switches: :dfn:`cooperative` and :dfn:`preemptive`.
211
212* A *cooperative* context switch happens when a thread willfully gives the
213  control to another thread. There are two cases where this happens
214
215  * When a thread explicitly yields.
216  * When a thread tries to take an object that is currently unavailable and is
217    willing to wait until the object becomes available.
218
219* A *preemptive* context switch happens either because an ISR or a
220  thread causes an operation that schedules a thread of higher priority than the
221  one currently running, if the currently running thread is preemptible.
222  An example of such an operation is releasing an object on which the thread
223  of higher priority was waiting.
224
225.. note::
226
227  Control is never taken from cooperative thread when one of them is the
228  running thread.
229
230A cooperative context switch is always done by having a thread call the
231:code:`_Swap()` kernel internal symbol. When :code:`_Swap` is called, the
232kernel logic knows that a context switch has to happen: :code:`_Swap` does not
233check to see if a context switch must happen. Rather, :code:`_Swap` decides
234what thread to context switch in. :code:`_Swap` is called by the kernel logic
235when an object being operated on is unavailable, and some thread
236yielding/sleeping primitives.
237
238.. note::
239
240  On x86 and Nios2, :code:`_Swap` is generic enough and the architecture
241  flexible enough that :code:`_Swap` can be called when exiting an interrupt
242  to provoke the context switch. This should not be taken as a rule, since
243  neither the ARM Cortex-M or ARCv2 port do this.
244
245Since :code:`_Swap` is cooperative, the caller-saved registers from the ABI are
246already on the stack. There is no need to save them in the k_thread structure.
247
248A context switch can also be performed preemptively. This happens upon exiting
249an ISR, in the kernel interrupt exit stub:
250
251* :code:`_interrupt_enter` on x86 after the handler is called.
252* :code:`_IntExit` on ARM.
253* :code:`_firq_exit` and :code:`_rirq_exit` on ARCv2.
254
255In this case, the context switch must only be invoked when the interrupted
256thread was preemptible, not when it was a cooperative one, and only when the
257current interrupt is not nested.
258
259The kernel also has the concept of "locking the scheduler". This is a concept
260similar to locking the interrupts, but lighter-weight since interrupts can
261still occur. If a thread has locked the scheduler, is it temporarily
262non-preemptible.
263
264So, the decision logic to invoke the context switch when exiting an interrupt
265is simple:
266
267* If the interrupted thread is not preemptible, do not invoke it.
268* Else, fetch the cached thread from the ready queue, and:
269
270  * If the cached thread is not the current thread, invoke the context switch.
271  * Else, do not invoke it.
272
273This is simple, but crucial: if this is not implemented correctly, the kernel
274will not function as intended and will experience bizarre crashes, mostly due
275to stack corruption.
276
277.. note::
278
279  If running a coop-only system, i.e. if :kconfig:option:`CONFIG_NUM_PREEMPT_PRIORITIES`
280  is 0, no preemptive context switch ever happens. The interrupt code can be
281  optimized to not take any scheduling decision when this is the case.
282
283Thread Creation and Termination
284*******************************
285
286To start a new thread, a stack frame must be constructed so that the context
287switch can pop it the same way it would pop one from a thread that had been
288context switched out. This is to be implemented in an architecture-specific
289:code:`_new_thread` internal routine.
290
291The thread entry point is also not to be called directly, i.e. it should not be
292set as the :abbr:`PC (program counter)` for the new thread. Rather it must be
293wrapped in :code:`_thread_entry`. This means that the PC in the stack
294frame shall be set to :code:`_thread_entry`, and the thread entry point shall
295be passed as the first parameter to :code:`_thread_entry`. The specifics of
296this depend on the ABI.
297
298The need for an architecture-specific thread termination implementation depends
299on the architecture. There is a generic implementation, but it might not work
300for a given architecture.
301
302One reason that has been encountered for having an architecture-specific
303implementation of thread termination is that aborting a thread might be
304different if aborting because of a graceful exit or because of an exception.
305This is the case for ARM Cortex-M, where the CPU has to be taken out of handler
306mode if the thread triggered a fatal exception, but not if the thread
307gracefully exits its entry point function.
308
309This means implementing an architecture-specific version of
310:c:func:`k_thread_abort`, and setting the Kconfig option
311:kconfig:option:`CONFIG_ARCH_HAS_THREAD_ABORT` as needed for the architecture (e.g. see
312:zephyr_file:`arch/arm/core/cortex_m/Kconfig`).
313
314Thread Local Storage
315********************
316
317To enable thread local storage on a new architecture:
318
319#. Implement :c:func:`arch_tls_stack_setup` to setup the TLS storage area in
320   stack. Refer to the toolchain documentation on how the storage area needs
321   to be structured. Some helper functions can be used:
322
323   * Function :c:func:`z_tls_data_size` returns the size
324     needed for thread local variables (excluding any extra data required by
325     toolchain and architecture).
326   * Function :c:func:`z_tls_copy` prepares the TLS storage area for
327     thread local variables. This only copies the variable themselves and
328     does not do architecture and/or toolchain specific data.
329
330#. In the context switching, grab the ``tls`` field inside the new thread's
331   ``struct k_thread`` and put it into an appropriate register (or some
332   other variable) for access to the TLS storage area. Refer to toolchain
333   and architecture documentation on which registers to use.
334#. In kconfig, add ``select CONFIG_ARCH_HAS_THREAD_LOCAL_STORAGE`` to
335   kconfig related to the new architecture.
336#. Run the ``tests/kernel/threads/tls`` to make sure the new code works.
337
338Device Drivers
339**************
340
341The kernel requires very few hardware devices to function. In theory, the only
342required device is the interrupt controller, since the kernel can run without a
343system clock. In practice, to get access to most, if not all, of the sanity
344check test suite, a system clock is needed as well. Since these two are usually
345tied to the architecture, they are part of the architecture port.
346
347Interrupt Controllers
348=====================
349
350There can be significant differences between the interrupt controllers and the
351interrupt concepts across architectures.
352
353For example, x86 has the concept of an :abbr:`IDT (Interrupt Descriptor Table)`
354and different interrupt controllers. The position of an interrupt in the IDT
355determines its priority.
356
357On the other hand, the ARM Cortex-M has the :abbr:`NVIC (Nested Vectored
358Interrupt Controller)` as part of the architecture definition. There is no need
359for an IDT-like table that is separate from the NVIC vector table. The position
360in the table has nothing to do with priority of an IRQ: priorities are
361programmable per-entry.
362
363The ARCv2 has its interrupt unit as part of the architecture definition, which
364is somewhat similar to the NVIC. However, where ARC defines interrupts as
365having a one-to-one mapping between exception and interrupt numbers (i.e.
366exception 1 is IRQ1, and device IRQs start at 16), ARM has IRQ0 being
367equivalent to exception 16 (and weirdly enough, exception 1 can be seen as
368IRQ-15).
369
370All these differences mean that very little, if anything, can be shared between
371architectures with regards to interrupt controllers.
372
373System Clock
374============
375
376x86 has APIC timers and the HPET as part of its architecture definition. ARM
377Cortex-M has the SYSTICK exception. Finally, ARCv2 has the timer0/1 device.
378
379Kernel timeouts are handled in the context of the system clock timer driver's
380interrupt handler.
381
382
383Console Over Serial Line
384========================
385
386There is one other device that is almost a requirement for an architecture
387port, since it is so useful for debugging. It is a simple polling, output-only,
388serial port driver on which to send the console (:code:`printk`,
389:code:`printf`) output.
390
391It is not required, and a RAM console (:kconfig:option:`CONFIG_RAM_CONSOLE`)
392can be used to send all output to a circular buffer that can be read
393by a debugger instead.
394
395Utility Libraries
396*****************
397
398The kernel depends on a few functions that can be implemented with very few
399instructions or in a lock-less manner in modern processors. Those are thus
400expected to be implemented as part of an architecture port.
401
402* Atomic operators.
403
404  * If instructions do exist for a given architecture, the implementation is
405    configured using the :kconfig:option:`CONFIG_ATOMIC_OPERATIONS_ARCH` Kconfig
406    option.
407
408  * If instructions do not exist for a given architecture,
409    a generic version that wraps :c:func:`irq_lock` or :c:func:`irq_unlock`
410    around non-atomic operations exists. It is configured using the
411    :kconfig:option:`CONFIG_ATOMIC_OPERATIONS_C` Kconfig option.
412
413* Find-least-significant-bit-set and find-most-significant-bit-set.
414
415  * If instructions do not exist for a given architecture, it is always
416    possible to implement these functions as generic C functions.
417
418It is possible to use compiler built-ins to implement these, but be careful
419they use the required compiler barriers.
420
421CPU Idling/Power Management
422***************************
423
424The kernel provides support for CPU power management with two functions:
425:c:func:`arch_cpu_idle` and :c:func:`arch_cpu_atomic_idle`.
426
427:c:func:`arch_cpu_idle` can be as simple as calling the power saving
428instruction for the architecture with interrupts unlocked, for example
429:code:`hlt` on x86, :code:`wfi` or :code:`wfe` on ARM, :code:`sleep` on ARC.
430This function can be called in a loop within a context that does not care if it
431get interrupted or not by an interrupt before going to sleep. There are
432basically two scenarios when it is correct to use this function:
433
434* In a single-threaded system, in the only thread when the thread is not used
435  for doing real work after initialization, i.e. it is sitting in a loop doing
436  nothing for the duration of the application.
437
438* In the idle thread.
439
440:c:func:`arch_cpu_atomic_idle`, on the other hand, must be able to atomically
441re-enable interrupts and invoke the power saving instruction. It can thus be
442used in real application code, again in single-threaded systems.
443
444Normally, idling the CPU should be left to the idle thread, but in some very
445special scenarios, these APIs can be used by applications.
446
447Both functions must exist for a given architecture. However, the implementation
448can be simply the following steps, if desired:
449
450#. unlock interrupts
451#. NOP
452
453However, a real implementation is strongly recommended.
454
455Fault Management
456****************
457
458In the event of an unhandled CPU exception, the architecture
459code must call into :c:func:`z_fatal_error`.  This function dumps
460out architecture-agnostic information and makes a policy
461decision on what to do next by invoking :c:func:`k_sys_fatal_error`.
462This function can be overridden to implement application-specific
463policies that could include locking interrupts and spinning forever
464(the default implementation) or even powering off the
465system (if supported).
466
467Toolchain and Linking
468*********************
469
470Toolchain support has to be added to the build system.
471
472Some architecture-specific definitions are needed in :zephyr_file:`include/zephyr/toolchain/gcc.h`.
473See what exists in that file for currently supported architectures.
474
475Each architecture also needs its own linker script, even if most sections can
476be derived from the linker scripts of other architectures. Some sections might
477be specific to the new architecture, for example the SCB section on ARM and the
478IDT section on x86.
479
480Memory Management and Memory Mapping
481************************************
482
483If the target platform enables paging and requires drivers to memory-map
484their I/O regions, :kconfig:option:`CONFIG_MMU` needs to be enabled and the
485following API implemented:
486
487- :c:func:`arch_mem_map`
488- :c:func:`arch_mem_unmap`
489- :c:func:`arch_page_phys_get`
490
491Stack Objects
492*************
493
494The presence of memory protection hardware affects how stack objects are
495created. All architecture ports must specify the required alignment of the
496stack pointer, which is some combination of CPU and ABI requirements. This
497is defined in architecture headers with :c:macro:`ARCH_STACK_PTR_ALIGN` and
498is typically something small like 4, 8, or 16 bytes.
499
500Two types of thread stacks exist:
501
502- "kernel" stacks defined with :c:macro:`K_KERNEL_STACK_DEFINE()` and related
503  APIs, which can host kernel threads running in supervisor mode or
504  used as the stack for interrupt/exception handling. These have significantly
505  relaxed alignment requirements and use less reserved data. No memory is
506  reserved for privilege elevation stacks.
507
508- "thread" stacks which typically use more memory, but are capable of hosting
509  thread running in user mode, as well as any use-cases for kernel stacks.
510
511If :kconfig:option:`CONFIG_USERSPACE` is not enabled, "thread" and "kernel" stacks are
512equivalent.
513
514Additional macros may be defined in the architecture layer to specify
515the alignment of the base of stack objects, any reserved data inside the
516stack object not used for the thread's stack buffer, and how to round up
517stack sizes to support user mode threads. In the absence of definitions
518some defaults are assumed:
519
520- :c:macro:`ARCH_KERNEL_STACK_RESERVED`: default no reserved space
521- :c:macro:`ARCH_THREAD_STACK_RESERVED`: default no reserved space
522- :c:macro:`ARCH_KERNEL_STACK_OBJ_ALIGN`: default align to
523  :c:macro:`ARCH_STACK_PTR_ALIGN`
524- :c:macro:`ARCH_THREAD_STACK_OBJ_ALIGN`: default align to
525  :c:macro:`ARCH_STACK_PTR_ALIGN`
526- :c:macro:`ARCH_THREAD_STACK_SIZE_ALIGN`: default round up to
527  :c:macro:`ARCH_STACK_PTR_ALIGN`
528
529All stack creation macros are defined in terms of these.
530
531Stack objects all have the following layout, with some regions potentially
532zero-sized depending on configuration. There are always two main parts:
533reserved memory at the beginning, and then the stack buffer itself. The
534bounds of some areas can only be determined at runtime in the context of
535its associated thread object. Other areas are entirely computable at build
536time.
537
538Some architectures may need to carve-out reserved memory at runtime from the
539stack buffer, instead of unconditionally reserving it at build time, or to
540supplement an existing reserved area (as is the case with the ARM FPU).
541Such carve-outs will always be tracked in ``thread.stack_info.start``.
542The region specified by	``thread.stack_info.start`` and
543``thread.stack_info.size`` is always fully accessible by a user mode thread.
544``thread.stack_info.delta`` denotes an offset which can be used to compute
545the initial stack pointer from the very end of the stack object, taking into
546account storage for TLS and ASLR random offsets.
547
548.. code-block:: none
549
550   +---------------------+ <- thread.stack_obj
551   | Reserved Memory     | } K_(THREAD|KERNEL)_STACK_RESERVED
552   +---------------------+
553   | Carved-out memory   |
554   |.....................| <- thread.stack_info.start
555   | Unused stack buffer |
556   |                     |
557   |.....................| <- thread's current stack pointer
558   | Used stack buffer   |
559   |                     |
560   |.....................| <- Initial stack pointer. Computable
561   | ASLR Random offset  |      with thread.stack_info.delta
562   +---------------------| <- thread.userspace_local_data
563   | Thread-local data   |
564   +---------------------+ <- thread.stack_info.start + thread.stack_info.size
565
566
567At present, Zephyr does not support stacks that grow upward.
568
569No Memory Protection
570====================
571
572If no memory protection is in use, then the defaults are sufficient.
573
574HW-based stack overflow detection
575=================================
576
577This option uses hardware features to generate a fatal error if a thread
578in supervisor mode overflows its stack. This is useful for debugging, although
579for a couple reasons, you can't reliably make any assertions about the state
580of the system after this happens:
581
582* The kernel could have been inside a critical section when the overflow
583  occurs, leaving important global data structures in a corrupted state.
584
585* For systems that implement stack protection using a guard memory region,
586  it's possible to overshoot the guard and corrupt adjacent data structures
587  before the hardware detects this situation.
588
589To enable the :kconfig:option:`CONFIG_HW_STACK_PROTECTION` feature, the system must
590provide some kind of hardware-based stack overflow protection, and enable the
591:kconfig:option:`CONFIG_ARCH_HAS_STACK_PROTECTION` option.
592
593Two forms of HW-based stack overflow detection are supported: dedicated
594CPU features for this purpose, or special read-only guard regions immediately
595preceding stack buffers.
596
597:kconfig:option:`CONFIG_HW_STACK_PROTECTION` only catches stack overflows for
598supervisor threads. This is not required to catch stack overflow from user
599threads; :kconfig:option:`CONFIG_USERSPACE` is orthogonal.
600
601This feature only detects supervisor mode stack overflows, including stack
602overflows when handling system calls. It doesn't guarantee that the kernel has
603not been corrupted. Any stack overflow in supervisor mode should be treated as
604a fatal error, with no assertions about the integrity of the overall system
605possible.
606
607Stack overflows in user mode are recoverable (from the kernel's perspective)
608and require no special configuration; :kconfig:option:`CONFIG_HW_STACK_PROTECTION`
609only applies to catching overflows when the CPU is in supervisor mode.
610
611CPU-based stack overflow detection
612----------------------------------
613
614If we are detecting stack overflows in supervisor mode via special CPU
615registers (like ARM's SPLIM), then the defaults are sufficient.
616
617
618
619Guard-based stack overflow detection
620------------------------------------
621
622We are detecting supervisor mode stack overflows via special memory protection
623region located immediately before the stack buffer that generates an exception
624on write. Reserved memory will be used for the guard region.
625
626:c:macro:`ARCH_KERNEL_STACK_RESERVED` should be defined to the minimum size
627of a memory protection region. On most ARM CPUs this is 32 bytes.
628:c:macro:`ARCH_KERNEL_STACK_OBJ_ALIGN` should also be set to the required
629alignment for this region.
630
631MMU-based systems should not reserve RAM for the guard region and instead
632simply leave an non-present virtual page below every stack when it is mapped
633into the address space. The stack object will still need to be properly aligned
634and sized to page granularity.
635
636.. code-block:: none
637
638   +-----------------------------+ <- thread.stack_obj
639   | Guard reserved memory       | } K_KERNEL_STACK_RESERVED
640   +-----------------------------+
641   | Guard carve-out             |
642   |.............................| <- thread.stack_info.start
643   | Stack buffer                |
644   .                             .
645
646Guard carve-outs for kernel stacks are uncommon and should be avoided if
647possible. They tend to be needed for two situations:
648
649* The same stack may be re-purposed to host a user thread, in which case
650  the guard is unnecessary and shouldn't be unconditionally reserved.
651  This is the case when privilege elevation stacks are not inside the stack
652  object.
653
654* The required guard size is variable and depends on context. For example, some
655  ARM CPUs have lazy floating point stacking during exceptions and may
656  decrement the stack pointer by a large value without writing anything,
657  completely overshooting a minimally-sized guard and corrupting adjacent
658  memory. Rather than unconditionally reserving a larger guard, the extra
659  memory is carved out if the thread uses floating point.
660
661User mode enabled
662=================
663
664Enabling user mode activates two new requirements:
665
666* A separate fixed-sized privilege mode stack, specified by
667  :kconfig:option:`CONFIG_PRIVILEGED_STACK_SIZE`, must be allocated that the user
668  thread cannot access. It is used as the stack by the kernel when handling
669  system calls. If stack guards are implemented, a stack guard region must
670  be able to be placed before it, with support for carve-outs if necessary.
671
672* The memory protection hardware must be able to program a region that exactly
673  covers the thread's stack buffer, tracked in ``thread.stack_info``. This
674  implies that :c:macro:`ARCH_THREAD_STACK_SIZE_ADJUST()` will need to round
675  up the requested stack size so that a region may cover it, and that
676  :c:macro:`ARCH_THREAD_STACK_OBJ_ALIGN()` is also specified per the
677  granularity of the memory protection hardware.
678
679This becomes more complicated if the memory protection hardware requires that
680all memory regions be sized to a power of two, and aligned to their own size.
681This is common on older MPUs and is known with
682:kconfig:option:`CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT`.
683
684``thread.stack_info`` always tracks the user-accessible part of the stack
685object, it must always be correct to program a memory protection region with
686user access using the range stored within.
687
688Non power-of-two memory region requirements
689-------------------------------------------
690
691On systems without power-of-two region requirements, the reserved memory area
692for threads stacks defined by :c:macro:`K_THREAD_STACK_RESERVED` may be used to
693contain the privilege mode stack. The layout could be something like:
694
695.. code-block:: none
696
697   +------------------------------+ <- thread.stack_obj
698   | Other platform data          |
699   +------------------------------+
700   | Guard region (if enabled)    |
701   +------------------------------+
702   | Guard carve-out (if needed)  |
703   |..............................|
704   | Privilege elevation stack    |
705   +------------------------------| <- thread.stack_obj +
706   | Stack buffer                 |      K_THREAD_STACK_RESERVED =
707   .                              .      thread.stack_info.start
708
709The guard region, and any carve-out (if needed) would be configured as a
710read-only region when the thread is created.
711
712* If the thread is a supervisor thread, the privilege elevation region is just
713  extra stack memory. An overflow will eventually crash into the guard region.
714
715* If the thread is running in user mode, a memory protection region will be
716  configured to allow user threads access to the stack buffer, but nothing
717  before or after it. An overflow in user mode will crash into the privilege
718  elevation stack, which the user thread has no access to. An overflow when
719  handling a system call will crash into the guard region.
720
721On an MMU system there should be no physical guards; the privilege mode stack
722will be mapped into kernel memory, and the stack buffer in the user part of
723memory, each with non-present virtual guard pages below them to catch runtime
724stack overflows.
725
726Other platform data may be stored before the guard region, but this is highly
727discouraged if such data could be stored in ``thread.arch`` somewhere.
728
729:c:macro:`ARCH_THREAD_STACK_RESERVED` will need to be defined to capture
730the size of the reserved region containing platform data, privilege elevation
731stacks, and guards. It must be appropriately sized such that an MPU region
732to grant user mode access to the stack buffer can be placed immediately
733after it.
734
735Power-of-two memory region requirements
736---------------------------------------
737
738Thread stack objects must be sized and aligned to the same power of two,
739without any reserved memory to allow efficient packing in memory. Thus,
740any guards in the thread stack must be completely carved out, and the
741privilege elevation stack must be allocated elsewhere.
742
743:c:macro:`ARCH_THREAD_STACK_SIZE_ADJUST()` and
744:c:macro:`ARCH_THREAD_STACK_OBJ_ALIGN()` should both be defined to
745:c:macro:`Z_POW2_CEIL()`. :c:macro:`K_THREAD_STACK_RESERVED` must be 0.
746
747For the privilege stacks, the :kconfig:option:`CONFIG_GEN_PRIV_STACKS` must be,
748enabled. For every thread stack found in the system, a corresponding fixed-size
749kernel stack used for handling system calls is generated. The address
750of the privilege stacks can be looked up quickly at runtime based on the
751thread stack address using :c:func:`z_priv_stack_find()`. These stacks are
752laid out the same way as other kernel-only stacks.
753
754.. code-block:: none
755
756   +-----------------------------+ <- z_priv_stack_find(thread.stack_obj)
757   | Reserved memory             | } K_KERNEL_STACK_RESERVED
758   +-----------------------------+
759   | Guard carve-out (if needed) |
760   |.............................|
761   | Privilege elevation stack   |
762   |                             |
763   +-----------------------------+ <- z_priv_stack_find(thread.stack_obj) +
764                                        K_KERNEL_STACK_RESERVED +
765                                        CONFIG_PRIVILEGED_STACK_SIZE
766
767   +-----------------------------+ <- thread.stack_obj
768   | MPU guard carve-out         |
769   | (supervisor mode only)      |
770   |.............................| <- thread.stack_info.start
771   | Stack buffer                |
772   .                             .
773
774The guard carve-out in the thread stack object is only used if the thread is
775running in supervisor mode. If the thread drops to user mode, there is no guard
776and the entire object is used as the stack buffer, with full access to the
777associated user mode thread and ``thread.stack_info`` updated appropriately.
778
779User Mode Threads
780*****************
781
782To support user mode threads, several kernel-to-arch APIs need to be
783implemented, and the system must enable the :kconfig:option:`CONFIG_ARCH_HAS_USERSPACE`
784option. Please see the documentation for each of these functions for more
785details:
786
787* :c:func:`arch_buffer_validate` to test whether the current thread has
788  access permissions to a particular memory region
789
790* :c:func:`arch_user_mode_enter` which will irreversibly drop a supervisor
791  thread to user mode privileges. The stack must be wiped.
792
793* :c:func:`arch_syscall_oops` which generates a kernel oops when system
794  call parameters can't be validated, in such a way that the oops appears to be
795  generated from where the system call was invoked in the user thread
796
797* :c:func:`arch_syscall_invoke0` through
798  :c:func:`arch_syscall_invoke6` invoke a system call with the
799  appropriate number of arguments which must all be passed in during the
800  privilege elevation via registers.
801
802* :c:func:`arch_is_user_context` return nonzero if the CPU is currently
803  running in user mode
804
805* :c:func:`arch_mem_domain_max_partitions_get` which indicates the max
806  number of regions for a memory domain. MMU systems have an unlimited amount,
807  MPU systems have constraints on this.
808
809Some architectures may need to update software memory management structures
810or modify hardware registers on another CPU when memory domain APIs are invoked.
811If so, :kconfig:option:`CONFIG_ARCH_MEM_DOMAIN_SYNCHRONOUS_API` must be selected by the
812architecture and some additional APIs must be implemented. This is common
813on MMU systems and uncommon on MPU systems:
814
815* :c:func:`arch_mem_domain_thread_add`
816
817* :c:func:`arch_mem_domain_thread_remove`
818
819* :c:func:`arch_mem_domain_partition_add`
820
821* :c:func:`arch_mem_domain_partition_remove`
822
823Please see the doxygen documentation of these APIs for details.
824
825In addition to implementing these APIs, there are some other tasks as well:
826
827* :c:func:`_new_thread` needs to spawn threads with :c:macro:`K_USER` in
828  user mode
829
830* On context switch, the outgoing thread's stack memory should be marked
831  inaccessible to user mode by making the appropriate configuration changes in
832  the memory management hardware.. The incoming thread's stack memory should
833  likewise be marked as accessible. This ensures that threads can't mess with
834  other thread stacks.
835
836* On context switch, the system needs to switch between memory domains for
837  the incoming and outgoing threads.
838
839* Thread stack areas must include a kernel stack region. This should be
840  inaccessible to user threads at all times. This stack will be used when
841  system calls are made. This should be fixed size for all threads, and must
842  be large enough to handle any system call.
843
844* A software interrupt or some kind of privilege elevation mechanism needs to
845  be established. This is closely tied to how the _arch_syscall_invoke macros
846  are implemented. On system call, the appropriate handler function needs to
847  be looked up in _k_syscall_table. Bad system call IDs should jump to the
848  :c:enum:`K_SYSCALL_BAD` handler. Upon completion of the system call, care
849  must be taken not to leak any register state back to user mode.
850
851GDB Stub
852********
853
854To enable GDB stub for remote debugging on a new architecture:
855
856#. Create a new ``gdbstub.h`` header file under appropriate architecture
857   include directory (``include/arch/<arch>/gdbstub.h``).
858
859   * Create a new struct ``struct gdb_ctx`` as the GDB context.
860
861     * Must define a member named ``exception`` of type ``unsigned int`` to
862       store the GDB exception reason. This value needs to be set before
863       entering :c:func:`z_gdb_main_loop`.
864
865     * Architecture can define as many members as needed for GDB stub to
866       function.
867
868     * Pointer to this struct needs to be passed to :c:func:`z_gdb_main_loop`,
869       where this pointer will be passed to other GDB stub functions.
870
871#. Functions for entering and exiting GDB stub main loop.
872
873   * If the architecture relies on interrupts to service breakpoints,
874     interrupt service routines (ISR) need to be implemented, which
875     will serve as the entry point to GDB stub main loop.
876
877   * These functions need to save and restore context so code execution
878     can continue as if no breakpoints have been encountered.
879
880   * These functions need to call :c:func:`z_gdb_main_loop` after saving
881     execution context to go into the GDB stub main loop to receive commands
882     from GDB.
883
884   * Before calling :c:func:`z_gdb_main_loop`, :c:member:`gdb_ctx.exception`
885     must be set to specify the exception reason.
886
887#. Implement necessary functions to support GDB stub functionality:
888
889   * :c:func:`arch_gdb_init`
890
891     * This needs to initialize necessary bits to support GDB stub functionality,
892       for example, setting up the GDB context and connecting debug interrupts.
893
894     * This must stop code execution via architecture specific method (e.g.
895       raising debug interrupts). This allows GDB to connect during boot.
896
897   * :c:func:`arch_gdb_continue`
898
899     * This function is called when GDB sends a ``c`` or ``continue`` command
900       to continue code execution.
901
902   * :c:func:`arch_gdb_step`
903
904     * This function is called when GDB sends a ``si`` or ``stepi`` command
905       to execute one machine instruction, before returning to GDB prompt.
906
907   * Hardware register read/write functions:
908
909     * Since the GDB stub is running on the target, manipulation of hardware
910       registers need to cached to avoid affecting the execution of GDB stub.
911       Think of it as context switching, where the execution context is
912       changed to the GDB stub. So that the register values of the running
913       thread before context switch need to be stored. Manipulation of
914       register values must only be done to this cached copy. The updated
915       values will then be written to hardware registers before switching
916       back to the previous running thread.
917
918     * :c:func:`arch_gdb_reg_readall`
919
920       * This collects all hardware register values that would appear in
921         a ``g``/``G`` packets which will be sent back to GDB. The format of
922         the G-packet is architecture specific. Consult GDB on what is
923         expected.
924
925       * Note that, for most architectures, a valid G-packet must be returned
926         and sent to GDB. If a packet without incorrect length is sent to
927         GDB, GDB will abort the debugging session.
928
929     * :c:func:`arch_gdb_reg_writeall`
930
931       * This takes a G-packet sent by GDB and populates the hardware
932         registers with values from the G-packet.
933
934     * :c:func:`arch_gdb_reg_readone`
935
936       * This reads the value of one hardware register and sends
937         the result to GDB.
938
939     * :c:func:`arch_gdb_reg_writeone`
940
941       * This writes the value of one hardware register received from GDB.
942
943   * Breakpoints:
944
945     * :c:func:`arch_gdb_add_breakpoint` and
946       :c:func:`arch_gdb_remove_breakpoint`
947
948     * GDB may decide to use software breakpoints which modifies
949       the memory at the breakpoint locations to replace the instruction
950       with software breakpoint or trap instructions. GDB will then
951       restore the memory content once execution reaches the breakpoints.
952       GDB supports this by default and there is usually no need to
953       handle software breakpoints in the architecture code (where
954       breakpoint type is ``0``).
955
956     * Hardware breakpoints (type ``1``) are required if the code is
957       in ROM or flash that cannot be modified at runtime. Consult
958       the architecture datasheet on how to enable hardware breakpoints.
959
960     * If hardware breakpoints are not supported by the architecture,
961       there is no need to implement these in architecture code.
962       GDB will then rely on software breakpoints.
963
964#. For architecture where certain memory regions are not accessible,
965   an array named :c:var:`gdb_mem_region_array` of type
966   :c:struct:`gdb_mem_region` needs to be defined to specify regions
967   that are accessible. For each array item:
968
969   * :c:member:`gdb_mem_region.start` specifies the start of a memory
970     region.
971
972   * :c:member:`gdb_mem_region.end` specifies the end of a memory
973     region.
974
975   * :c:member:`gdb_mem_region.attributes` specifies the permission
976     of a memory region.
977
978     * :c:macro:`GDB_MEM_REGION_RO`: region is read-only.
979
980     * :c:macro:`GDB_MEM_REGION_RW`: region is read-write.
981
982   * :c:member:`gdb_mem_region.alignment` specifies read/write alignment
983     of a memory region. Use ``0`` if there is no alignment requirement
984     and read/write can be done byte-by-byte.
985
986API Reference
987*************
988
989Timing
990======
991
992.. doxygengroup:: arch-timing
993
994Threads
995=======
996
997.. doxygengroup:: arch-threads
998
999.. doxygengroup:: arch-tls
1000
1001Power Management
1002================
1003
1004.. doxygengroup:: arch-pm
1005
1006Symmetric Multi-Processing
1007==========================
1008
1009.. doxygengroup:: arch-smp
1010
1011Interrupts
1012==========
1013
1014.. doxygengroup:: arch-irq
1015
1016Userspace
1017=========
1018
1019.. doxygengroup:: arch-userspace
1020
1021Memory Management
1022=================
1023
1024.. doxygengroup:: arch-mmu
1025
1026Miscellaneous Architecture APIs
1027===============================
1028
1029.. doxygengroup:: arch-misc
1030
1031GDB Stub APIs
1032=============
1033
1034.. doxygengroup:: arch-gdbstub
1035