# Bootstrap

---

# Bootstrap

_Peios Kernel / Internals / LCS / Bootstrap_

> Find the Peios kernel articles about bootstrap.

Use these articles to understand bootstrap in the Peios kernel.

- [Dependencies and boot sequence](</kernel/internals/lcs/bootstrap/dependencies-and-boot-sequence.md>)
- [Operational Parameters](</kernel/internals/lcs/bootstrap/operational-parameters.md>)
- [The Self-Watch](</kernel/internals/lcs/bootstrap/the-self-watch.md>)

---

# Dependencies and boot sequence

_Peios Kernel / Internals / LCS / Bootstrap_

> The registry has to configure itself — the three circular dependencies, and the three rules that break them.

<a id="trm-5-10-1"></a>

The registry is the configuration store for the whole system, and it
has to configure itself. Three circular dependencies have to be broken
before it can serve anyone.

**peinit needs the registry to start services, but the registry source
is a service.** Service definitions live in the registry, and the
process that would serve them has not started.

**LCS needs operational parameters from the registry, but the registry
needs LCS.** Timeouts, caps and limits live under
`Machine\System\Registry\`, in a hive LCS itself routes.

**A fresh install has no data at all.** The source's database is empty;
there is nothing to read.

Three rules break all three.

<a id="trm-5-10-1-1"></a>

## Rule 1: compiled-in defaults

Every operational parameter has a compiled-in default, and LCS runs on
those defaults from the moment PKM initialises. <a id="cite-boot.defaults-active-from-init"></a>

There is no "waiting for configuration" state, no flag, and no wait
queue: the limits structure is statically initialised at load, the
source char device is registered without consulting configuration, and
the first attempt to read configuration happens on a workqueue after a
source has already registered. <a id="cite-boot.no-waiting-for-configuration-state"></a>

Before any source registers, the routing table is empty, so every
operation that names a hive returns `ENOENT`. <a id="cite-boot.empty-routing-table-enoent"></a>
That is the only sense in which LCS is not yet useful, and it is not a
distinct state — it is just an empty table.

<a id="trm-5-10-1-2"></a>

## Rule 2: the base layer exists unconditionally

The base layer is a static constant in the kernel: name `base`,
precedence 0, enabled. It is handed out whenever the dynamic layer
table is empty and is always written first into every layer snapshot.

A source that registers with an entirely empty database is fully
functional, because the one layer that writes need is not in the
database. Persisted metadata under
`Machine\System\Registry\Layers\base\` may exist and may decorate the
base layer, but it is not required and cannot contradict it ([The Base Layer](</kernel/internals/lcs/layers/the-base-layer.md#trm-5-3-2>)).

<a id="trm-5-10-1-3"></a>

## Rule 3: hot-swap, not restart <a id="cite-boot.hot-swap-not-restart"></a>

When configuration becomes available, LCS reads it, validates it, and
swaps the values in place. It does not restart, re-initialise, or
block. The whole transition from compiled-in defaults to registry-backed
configuration is driven by the internal self-watch ([The Self-Watch](</kernel/internals/lcs/bootstrap/the-self-watch.md#trm-5-10-4>)).

<a id="trm-5-10-1-4"></a>

## Source dependencies are the source's problem <a id="cite-boot.source-dependencies-not-lcs-concern"></a>

LCS neither knows nor cares what a source depends on. A source that
needs the root filesystem, a SYSTEM token, or a particular kernel
feature arranges that for itself. LCS's only requirement is that the
process can open `/dev/pkm_registry` and speak RSI.

<a id="trm-5-10-2"></a>

<a id="trm-5-10-2-1"></a>

## Normal boot <a id="cite-boot.normal-sequence"></a>

```
Kernel boots
  → PKM initialises; LCS runs on compiled-in defaults
  → /dev/pkm_registry registered
  → the base layer exists in memory

A source registers — loregd, started by peinit
  → opens /dev/pkm_registry (SeTcbPrivilege checked at open)
  → REG_SRC_REGISTER: hive names, root GUIDs, max persisted sequence
  → LCS initialises or advances next_sequence to max + 1

Bootstrap refresh is queued
  → resolve Machine\System\Registry     → read and hot-swap parameters
  → resolve Machine\System\Registry\Layers → populate the layer table
  → arm internal subtree watches
```

The bootstrap refresh runs on a workqueue after `REG_SRC_REGISTER`
returns, so registration never blocks on configuration and a source
that registers can start answering immediately. <a id="cite-boot.refresh-on-workqueue-after-register"></a>

The refresh is triggered by the arrival of a **global hive named
`Machine`**. <a id="cite-boot.refresh-triggered-by-machine-hive"></a> That name is matched case-insensitively in the
kernel and is one of the two hive names LCS knows about; the other is
`Users`, the target of `CurrentUser\` rewriting ([Hives and Routing](</kernel/internals/lcs/data-model/hives-and-routing.md#trm-5-2-1>)). Neither is a
routing decision — routing is entirely dynamic — but the claim that the
kernel holds no hive names at all would not be true.

<a id="trm-5-10-2-2"></a>

## First boot <a id="cite-boot.first-boot-retains-defaults"></a>

An empty source has no `Machine\System\Registry` to read.

```
Source detects an empty database on first startup
  → generates root GUIDs for the hives it backs
  → creates root key records with their default SDs
  → persists them, then registers with LCS

LCS resolves Machine\System\Registry → does not exist
  → compiled-in defaults retained
  → subtree watch armed on the Machine\ hive root instead

peinit notices the registry is empty and restores the seed backup.
That is peinit's decision, not LCS's.

Seed restore populates Machine\ through REG_IOC_RESTORE
  → the fallback subtree watch fires
  → LCS re-runs the bootstrap refresh: resolves the specific GUIDs,
    re-arms targeted watches, validates and hot-swaps the seed values,
    and re-reads layer metadata
```

Hive root Security Descriptors are set by the source, not by LCS. LCS
holds no template for them and enforces whatever the source stores
([Inheritance and Hive Roots](</kernel/internals/lcs/security/inheritance-and-hive-roots.md#trm-5-4-3>)).

<a id="trm-5-10-2-3"></a>

## Watch arming in practice

The spec-level description above says the fallback watch is armed
*instead of* the targeted ones. What the kernel actually arms is a
**mixed** set: targeted watches on whichever of the two roots exist,
*plus* the `Machine\` root fallback. The fallback is a superset rather
than a substitute, and it stays armed until a refresh finds both roots
present.

A third internal watch is armed by the same sequence, on
`Machine\System\KMES`, which is not LCS configuration at all — it is
how KMES picks up its own parameters from the registry LCS serves.

<a id="trm-5-10-2-4"></a>

## The bootstrap contract

Four properties hold throughout and are relied on elsewhere:

1. **LCS is always operational with compiled-in defaults.** It accepts
   syscalls from the moment PKM initialises.
2. **The base layer requires no persisted state.**
3. **Hot-swap is the only configuration transition.** LCS never blocks,
   restarts, or re-initialises.
4. **Source dependencies are the source's concern.**

---

# Operational Parameters

_Peios Kernel / Internals / LCS / Bootstrap_

> The nineteen parameters LCS reads from the registry, how they are validated, and what hot-swapping one does to in-flight operations.

<a id="trm-5-10-3"></a>

LCS reads nineteen parameters from `Machine\System\Registry\`. <a id="cite-param.nineteen-under-registry-key"></a>

All are `REG_DWORD`. <a id="cite-param.all-are-reg-dword"></a> Each has a compiled-in default and a valid
range, and LCS runs on the defaults until the registry says otherwise.

| Value | Default | Range | Bounds |
|---|---:|---|---|
| `RequestTimeoutMs` | 30000 | 1000–600000 | A source round trip ([Request Dispatch](</kernel/internals/lcs/sources/request-dispatch.md#trm-5-8-3>)). <a id="cite-param.request-timeout-ms"></a> |
| `TransactionTimeoutMs` | 30000 | 1000–600000 | The lifetime of an open transaction ([Scope and Lifetime](</kernel/internals/lcs/transactions/scope-and-lifetime.md#trm-5-7-1>)). <a id="cite-param.transaction-timeout-ms"></a> |
| `NotificationQueueSize` | 256 | 16–65536 | Queued events per watcher before overflow ([Queues and Overflow](</kernel/internals/lcs/watches/queues-and-overflow.md#trm-5-6-4>)). <a id="cite-param.notification-queue-size"></a> |
| `SymlinkDepthLimit` | 16 | 1–64 | Symlink resolution depth ([Symlinks](</kernel/internals/lcs/data-model/symlinks.md#trm-5-2-4>)). <a id="cite-param.symlink-depth-limit"></a> |
| `MaxValueSize` | 1048576 | 4096–67108864 | One value's data, in bytes. <a id="cite-param.max-value-size"></a> |
| `MaxKeyDepth` | 512 | 32–4096 | Key hierarchy nesting. <a id="cite-param.max-key-depth"></a> |
| `MaxPathComponentLength` | 255 | 64–1024 | One key, value or layer name, in UTF-8 bytes. <a id="cite-param.max-path-component-length"></a> |
| `MaxTotalPathLength` | 16383 | 1024–65535 | A whole path, in UTF-8 bytes. <a id="cite-param.max-total-path-length"></a> |
| `MaxLayersPerValue` | 128 | 1–1024 | Layers writing to one `(key, value name)` ([The Layer Model](</kernel/internals/lcs/layers/the-layer-model.md#trm-5-3-1>)). <a id="cite-param.max-layers-per-value"></a> |
| `MaxBoundTransactionsPerSource` | 16 | 1–256 | Concurrently bound transactions per source ([Scope and Lifetime](</kernel/internals/lcs/transactions/scope-and-lifetime.md#trm-5-7-1>)). <a id="cite-param.max-bound-transactions-per-source"></a> |
| `MaxReadOnlyTransactionsPerSource` | 16 | 1–256 | Concurrent public read transactions and backup snapshots per source ([Backup](</kernel/internals/lcs/backup-and-restore/backup.md#trm-5-9-2>)). <a id="cite-param.max-read-only-transactions-per-source"></a> |
| `MaxTotalLayers` | 1024 | 16–65536 | Distinct layers in the in-memory table ([The Layer Model](</kernel/internals/lcs/layers/the-layer-model.md#trm-5-3-1>)). <a id="cite-param.max-total-layers"></a> |
| `MaxRegisteredSources` | 32 | 1–256 | Concurrently registered sources. <a id="cite-param.max-registered-sources"></a> |
| `MaxHivesPerSource` | 64 | 1–1024 | Hives one source may register. <a id="cite-param.max-hives-per-source"></a> |
| `MaxConcurrentRSIRequests` | 256 | 8–4096 | In-flight RSI requests per source ([Request Dispatch](</kernel/internals/lcs/sources/request-dispatch.md#trm-5-8-3>)). <a id="cite-param.max-concurrent-rsi-requests"></a> |
| `MaxScopeGUIDsPerToken` | 8 | 1–256 | Private hive scope GUIDs on a token ([Private Hives](</kernel/internals/lcs/data-model/private-hives.md#trm-5-2-2>)). <a id="cite-param.max-scope-guids-per-token"></a> |
| `MaxPrivateLayersPerToken` | 16 | 1–256 | Private layer names on a token ([Private Layers](</kernel/internals/lcs/layers/private-layers.md#trm-5-3-5>)). <a id="cite-param.max-private-layers-per-token"></a> |
| `MaxSubtreeWatchDepth` | 0 | 0–4096 | Subtree watch depth; 0 is unlimited ([Dispatch](</kernel/internals/lcs/watches/dispatch.md#trm-5-6-3>)). <a id="cite-param.max-subtree-watch-depth"></a> |
| `MaxTransactionWatchEventBurst` | 4096 | 256–65536 | Watch events per watcher from one commit ([Dispatch](</kernel/internals/lcs/watches/dispatch.md#trm-5-6-3>)). <a id="cite-param.max-transaction-watch-event-burst"></a> |

Unknown values under this key are ignored. <a id="cite-param.unknown-values-ignored"></a> There are exactly
nineteen parameters and no undocumented ones; a separate set under
`Machine\System\KMES\` belongs to KMES.

Two limits are not among them. The **transaction mutation log** is
capped at 4096 entries by a compile-time constant ([Isolation and the Mutation Log](</kernel/internals/lcs/transactions/isolation-and-the-mutation-log.md#trm-5-7-2>)), and hard
ceilings on total path length and key depth exist independently of
configuration — set equal to the range maxima above, so they never
conflict. <a id="cite-param.hard-ceilings-match-range-maxima"></a>

<a id="trm-5-10-3-1"></a>

## Where a configured value does not fully bind

One of the nineteen does not do everything its range suggests.

`MaxPrivateLayersPerToken` is described as an attachment-time limit but
is not enforced at attachment. KACS applies its own hard cap of 256 and
LCS applies the configured value later, at use, with `E2BIG` ([Private Layers](</kernel/internals/lcs/layers/private-layers.md#trm-5-3-5>)).
`MaxScopeGUIDsPerToken` behaves the same way.

<a id="trm-5-10-3-2"></a>

## Validation

A value is checked against its range when it is read.

- **Valid** — hot-swapped into the in-memory configuration and used by
  new operations. <a id="cite-param.validation.valid-is-hot-swapped"></a>
- **Invalid** — out of range, the wrong type, or missing — the value is
  **ignored** and the previously active one is kept: the compiled-in
  default or the last known-good. <a id="cite-param.validation.invalid-is-ignored-previous-kept"></a>
  An `lcs.config.value.rejected` audit event is emitted naming the
  parameter, what was wrong, and the value being retained ([Audit](</kernel/internals/lcs/security/audit.md#trm-5-4-4>)).

**Values are never clamped or silently corrected.** <a id="cite-param.validation.never-clamped"></a> There is no
`min`/`max` on any configuration path. A write to the registry
succeeds, because the source does not enforce kernel semantics, and LCS
simply refuses to use it. The registry shows what was written; the
audit log shows what LCS is running on.

Because "missing" is invalid, a first boot before seed restore emits
nineteen of these events per refresh — provided `Machine\System\Registry`
itself exists. If the key is absent there is nothing to read and
nothing is reported; the fallback watch of [The Self-Watch](</kernel/internals/lcs/bootstrap/the-self-watch.md#trm-5-10-4>) waits for it.

<a id="trm-5-10-3-3"></a>

## Hot-swap and in-flight operations

Configuration is published as a whole structure under a seqlock, and a
reader takes a complete copy of it. <a id="cite-param.hot-swap.published-as-one-structure"></a>

A syscall entry point snapshots it once and threads that snapshot
through the operation, so in-flight work uses the values that were
current when it started and new work uses the updated ones. <a id="cite-param.hot-swap.syscall-snapshots-once"></a>

That is the rule, and mostly the practice. Some deeper paths take a
second snapshot part-way through, and a few call sites read a single
live value rather than a snapshot — `reg_begin_transaction`'s timeout,
the bound-transaction cap, and the in-flight request cap among them.
For those, a hot-swap can be observed mid-operation. <a id="cite-param.hot-swap.some-call-sites-read-live-values"></a>

<a id="trm-5-10-3-4"></a>

## Security

`Machine\System\Registry\` inherits the `Machine` hive root descriptor
— SYSTEM and Administrators with `KEY_ALL_ACCESS`, Authenticated Users
with `KEY_READ` — so an unprivileged process cannot change any of
this. <a id="cite-param.security.unprivileged-cannot-change"></a>

Domain policy at a higher-precedence layer defends against a
compromised local administrator, which is the reason `SeTcbPrivilege`
guards precedence above 0 ([Writing Into a Layer](</kernel/internals/lcs/layers/writing-into-a-layer.md#trm-5-3-4>)).

## Operator verification

Inspect the stored configuration and installed manual before tuning LCS:

```sh
reg get Machine/System/Registry
regman 'Machine\System\Registry'
```

Keep a recovery value. A successful write confirms storage, not adoption;
inspect `lcs.config.value.rejected` events as well. Deleting a parameter does
not reset an already accepted running value. Write the intended valid value
and verify it at the documented application point.

An unavailable hive is a source problem, distinct from an absent parameter.
For loregd, inspect the output captured by Peinit before readiness and the
stored service logs after logging starts. Missing configuration alone is not
evidence that a database needs replacement or reseeding.

---

# The Self-Watch

_Peios Kernel / Internals / LCS / Bootstrap_

> LCS watches its own configuration through the same machinery userspace uses — what it drives, how it is armed, and why the callback is not the atomicity boundary.

<a id="trm-5-10-4"></a>

LCS watches its own configuration and layer metadata subtrees, and it
does so through the same machinery userspace uses — but not through the
same interface.

An internal watch is an entry in the same watch map, taking a reference
on the same subtree watch set, distinguished only by a kind marker. It
has **no fd, no granted access mask and no filter**. <a id="cite-self-watch.no-fd-mask-or-filter"></a>

Events reach it through a kernel callback rather than being queued for
a `read()`, and it is therefore not subject to
`NotificationQueueSize`. <a id="cite-self-watch.exempt-from-notification-queue-size"></a>

It is also not subject to `MaxSubtreeWatchDepth`, nor to the
transaction burst suppressor: internal collection happens before either
test. <a id="cite-self-watch.exempt-from-depth-and-burst-limits"></a>

Because there is no filter, deliverability is decided per target rather
than by a bitmask. Each internal target admits only the event types it
cares about: value events on the watched key itself for the
configuration subtrees, and subkey events at depth 0 or value and
descriptor events at depth 1 for the layer metadata subtree. <a id="cite-self-watch.admits-event-types-per-target"></a>

<a id="trm-5-10-4-1"></a>

## What it drives

**Self-configuration.** A change under `Machine\System\Registry\`
triggers a re-read and validation of the parameters ([Operational Parameters](</kernel/internals/lcs/bootstrap/operational-parameters.md#trm-5-10-3>)). <a id="cite-self-watch.drives.parameter-re-read"></a>

**The layer table.** A change under `Machine\System\Registry\Layers\`
marks the affected layer names dirty and drives a bounded refresh of
their precedence, enabled state, owner and cached descriptor. Within
one delivery — a transaction's commit, say, whose batch creates a
layer and then writes its three values — each dirty layer is refreshed
once, however many of the batch's events named it. <a id="cite-self-watch.layer-refreshed-once-per-delivery"></a>

**Layer lifecycle.** `SUBKEY_CREATED` and `SUBKEY_DELETED` under
`Layers\` add and remove layers, except for `base`, which is ignored
([The Base Layer](</kernel/internals/lcs/layers/the-base-layer.md#trm-5-3-2>)). <a id="cite-self-watch.drives.layer-lifecycle-from-subkey-events"></a>

**KMES configuration.** A third internal watch, on
`Machine\System\KMES\`, exists for KMES's own parameters. <a id="cite-self-watch.drives.kmes-configuration"></a> It is not
LCS configuration, but the registry is where it lives and this is the
mechanism that notices it change.

<a id="trm-5-10-4-2"></a>

## The callback is not the atomicity boundary

For layer metadata, internal delivery identifies which layer names are
dirty. It does not itself publish anything, and it must not: publishing
a layer means publishing its table entry, metadata key GUID and cached
descriptor together ([Layer Metadata](</kernel/internals/lcs/layers/layer-metadata.md#trm-5-3-3>)), and a callback that published a partial
entry would create a window in which a layer exists and nobody can be
authorised against it. <a id="cite-self-watch.callback-does-not-publish"></a>

LCS also does not perform source round trips while holding the
watch-map or layer-table publication locks. <a id="cite-self-watch.no-source-round-trip-under-locks"></a> The refresh runs outside
them, after the mutating operation commits and before the syscall
returns.

<a id="trm-5-10-4-3"></a>

## Arming

At bootstrap, LCS resolves the GUIDs for `Machine\System\Registry\` and
`Machine\System\Registry\Layers\` through `RSI_LOOKUP` and arms
targeted subtree watches. <a id="cite-self-watch.arming.targeted-via-rsi-lookup"></a>

If either does not exist — first boot, empty database — it arms a
subtree watch on the `Machine\` hive root instead, so that seed restore
creating the subtree is noticed. <a id="cite-self-watch.arming.fallback-on-machine-root"></a>

That fallback event re-enters the whole bootstrap refresh, which
resolves the specific GUIDs and arms the targeted watches. <a id="cite-self-watch.arming.fallback-event-re-enters-refresh"></a>

In practice the kernel arms both: targeted watches for whichever roots
exist, **plus** the `Machine\` root fallback, until a refresh finds
everything present. <a id="cite-self-watch.arming.mixed-superset"></a> It is a superset of what is needed rather
than a substitute for it.

The same arming covers every key the kernel reads for itself, not only
LCS's own: KMES configuration, the port reservation table
(`Machine\System\Network\TcpIp\PortReservations\`, see the KACS
chapter on network objects), the network policy key
(`Machine\System\Network\`, whose rules and inventory the packet
engine reads — see the network policy chapter) and the event emission
policy (`Machine\Generic\Events\`, see the KMES chapter) are
discovered in the same refresh and get the same targeted-or-fallback
treatment. <a id="cite-self-watch.arming.covers-every-kernel-read-key"></a>

The two policy watches are the depth-unbounded, every-mutation ones.
For the network policy, rules are keys and exceptions are subkeys, so
anything written anywhere beneath the key may be policy. <a id="cite-self-watch.arming.policy-watch-is-depth-unbounded"></a>
The emission policy has a key per event-type segment at any depth, and
its watch passes on only changes on `Events` itself, beneath the
kernel's own roots, and to `Enabled` values (KMES [Emission Policy](</kernel/internals/kmes/emission-policy.md#trm-2-8>)).

**A refresh that fails still arms the fallback.** A stage can fail
transiently — a source answering a lookup while a concurrent write has
half-created a key was the case that found this — and if the refresh
simply returned, no watch would exist and no kernel-read key would load
for the life of the boot. So when any stage before arming fails, LCS
arms the `Machine\` root fallback alone: the next subkey creation under
the hive re-enters the refresh, which then arms the targeted watches as
above. <a id="cite-self-watch.arming.failed-refresh-arms-fallback-alone"></a>

The failure and the fallback arm are both traced by
`lcs:lcs_bootstrap_refresh`. <a id="cite-self-watch.arming.failure-is-traced"></a>

<a id="trm-5-10-4-4"></a>

## Bootstrap interaction

1. A source registers. LCS reads `Machine\System\Registry\*`; the keys
   do not exist; compiled-in defaults are retained.
2. Seed restore populates them. The subtree watch fires, LCS validates
   and hot-swaps to the seed values.
3. Subsequent administrative changes fire the watch again, and LCS
   validates and hot-swaps, or rejects with an audit event.

At no point is there a state in which LCS is waiting for
configuration.
