Skip to content

Commit e322ef8

Browse files
authored
src/mte_tag: tighten the first stage page table conditions for MTAG (#79)
Quite often loader or other piece of code might read code pages. If a code page is set as MTAG=1, then those loads (to code page) will look for tags. This change clarifies in specification that loads/stores to pages marked as executable always generate unchecked memory accesses. If if memory tagging is not enabled for execution environment then `MTAG` bit is ignored. This allows for setting up tagging and enabling/disabling on sampling basis (without asking OS to clear to the bit when tagging is disabled) Signed-off-by: Deepak Gupta <debug@rivosinc.com>
1 parent 38325f1 commit e322ef8

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

src/mte_tag.adoc

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ stores are subject to tag checks.
279279

280280
If memory tagging is enabled in the current execution environment and
281281
`satp.MODE != Bare`, then a regular load and regular store is subject to tag
282-
checks only if the page is marked as a tagged page (see <<TAGGED_PAGE>>) in the
283-
first stage page table.
282+
checks only if the data page is a tagged-data page and load/store instruction
283+
is on a tagcheck-exempt code page (see <<TAGGED_PAGE>>).
284284

285285
==== tag checks
286286
Once a regular load/store is determined (after paging bit checks and *envcfg
@@ -335,40 +335,41 @@ virtual address specified in `rs1`.
335335
[[TAGGED_PAGE]]
336336
=== Tag checks on page basis
337337

338-
`Zimt` introduces `memory tag` (`MTAG`) bit in first stage page table which if
339-
set in page table entry and memory tagging is enabled from *envcfg CSR,
340-
following rules apply:
338+
`Zimt` introduces `memory tag` (`MTAG`) bit in first stage page table which
339+
defines "tagged-data page" and "tagcheck-exempt code page".
341340

342-
1. tag checks are enforced on regular load/store to such page (tagged data
343-
page). See <<TAG_CHECKS>> for further checks. Underlying tagged page must
344-
be an idempotent memory else tag look up for referenced virtual memory will
345-
result in load access-fault exception.
341+
If paging is enabled, "tagged-data page" is a page translated using page tables
342+
with MTAG bit set and execute permission bit clear in first stage page tables.
343+
If paging is disabled and memory tagging is enabled for execution environment,
344+
then all data pages are "tagged-data page".
346345

347-
2. fetched instructions from a code page with `MTAG=1` (tag-exempt code page)
348-
generate unchecked load and store. This doesn't have any impact on behavior
349-
of `settag`, `gentag` and `addtag` instructions.
346+
If paging is enabled, a "tagcheck-exempt code page" is a page translated using
347+
page tables with MTAG bit set and execute permission bit set in first stage
348+
page tables. If paging is disabled then "tagcheck-exempt code page" are not
349+
applicable and do not exist.
350350

351-
3. If both rule 2 and rule 1 are applying, rule 2 takes precedence.
351+
If memory tagging is enabled for the execution environment from *envcfg CSR,
352+
following rules apply for checked memory accesses:
352353

353-
4. An instruction crossing a page boundary with differing `MTAG` value, the
354-
instruction behaves as if it was fetched from a page with MTAG=0.
354+
1. Memory accesses to a page which is not a "tagged-data page" always generate
355+
unchecked memory accesses.
355356

356-
`MTAG` bit in page table entry remains a reserved bit if `XWR == 111` or
357-
`XWR == 010` and if set, will raise a page fault of original access type.
357+
2. Memory accesses to a "tagged-data page" may generate checked memory
358+
accesses.
358359

359-
If memory tagging is not enabled for the execution environment via *envcfg CSR,
360-
then `MTAG` bit in page table entry remains a reserved and if set will raise a
361-
page fault of original access type.
360+
3. Instructions fetched from "tagcheck-exempt code page" always generate
361+
unchecked memory accesses.
362362

363-
[NOTE]
364-
====
365-
`MTAG` bit on data pages allows software to opt into selected memory regions
366-
to generate checked loads and stores. Furthermore, `MTAG` bit on executable
367-
pages allows software to opt out certain code regions from being subject to
368-
checked loads and stores. From usability point of view, shadow stack memory
369-
accesses or self modifying code do not need `MTAG` bit. Thus `MTAG` bit is
370-
kept as reserved for such page table encodings.
371-
====
363+
4. If both rule 3 and rule 2 are apply, rule 3 shall take precedence.
364+
365+
5. An instruction straddling between "tagcheck-exempt code page" and normal
366+
code page is assumed to be fetched from normal code page.
367+
368+
6. A load/store straddling between "tagged-data page" and normal page is
369+
assumed as "tagged-data page" for both pages.
370+
371+
If memory tagging is not enabled for the execution environment via *envcfg CSR,
372+
then `MTAG` bit in page table entry is ignored by hart.
372373

373374
==== Tag checks on supervisor accesses to user pages
374375

0 commit comments

Comments
 (0)