Basic implementation to support existing memory cache types for
non-PAT processors. PAT enabled processors also gain WC (Write
Combining) and WP (Write Protected) per-page memory cache types.
---
i386/Makefrag.am | 2 +
i386/i386/mp_desc.c | 3 +
i386/i386/pat.c | 140 ++++++++++++++++++++++++++++++++++
i386/i386/pat.h | 86 +++++++++++++++++++++
i386/i386at/acpi_parse_apic.c | 15 ++++
i386/i386at/model_dep.c | 8 +-
i386/intel/pmap.c | 3 +-
i386/intel/pmap.h | 13 ++++
linux/dev/glue/kmem.c | 2 +
vm/pmap.h | 1 +
vm/vm_kern.c | 9 ++-
vm/vm_kern.h | 3 +-
x86_64/Makefrag.am | 2 +
13 files changed, 278 insertions(+), 9 deletions(-)
create mode 100644 i386/i386/pat.c
create mode 100644 i386/i386/pat.h
diff --git a/i386/Makefrag.am b/i386/Makefrag.am
index b1151ab9..2c1c080f 100644
--- a/i386/Makefrag.am
+++ b/i386/Makefrag.am
@@ -30,6 +30,8 @@ if HOST_ix86
#
libkernel_a_SOURCES += \
+ i386/i386/pat.c \
+ i386/i386/pat.h \
i386/i386at/autoconf.c \
i386/i386at/autoconf.h \
i386/i386at/biosmem.c \
diff --git a/i386/i386/mp_desc.c b/i386/i386/mp_desc.c
index 8f2589b3..303a7bd4 100644
--- a/i386/i386/mp_desc.c
+++ b/i386/i386/mp_desc.c
@@ -44,6 +44,7 @@
#include <i386at/idt.h>
#include <i386at/int_init.h>
#include <i386/cpu.h>
+#include <i386/pat.h>
#include <i386/smp.h>
#include <i386at/model_dep.h>
@@ -257,6 +258,8 @@ cpu_setup(int cpu)
ap_ktss_init(cpu);
printf("AP=(%u) ktss done\n", cpu);
+ x86_pat_copy_boot_cpu();
+
/* Initialize machine_slot fields with the cpu data */
machine_slot[cpu].cpu_subtype = CPU_SUBTYPE_AT386;
machine_slot[cpu].cpu_type = machine_slot[0].cpu_type;
diff --git a/i386/i386/pat.c b/i386/i386/pat.c
new file mode 100644
index 00000000..54676299
--- /dev/null
+++ b/i386/i386/pat.c
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) 2026 Free Software Foundation, Inc.
+ *
+ * Permission to use, copy, modify and distribute this software and its
+ * documentation is hereby granted, provided that both the copyright
+ * notice and this permission notice appear in all copies of the
+ * software, derivative works or modified versions, and any portions
+ * thereof, and that both notices appear in supporting documentation.
+ *
+ * THE FREE SOFTWARE FOUNDATION ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
+ * CONDITION. THE FREE SOFTWARE FOUNDATION DISCLAIMS ANY LIABILITY OF ANY KIND
+ * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
+ */
+
+#include <i386/locore.h>
+#include <i386/msr.h>
+#include <i386/pat.h>
+#include <kern/debug.h>
+#include <stdint.h>
+#include <string.h>
+
+static boolean_t pat_support = 0;
+#ifndef MACH_XEN
+static uint64_t boot_cpu_pat;
+#endif
+
+#define X86_PAT_NUMTYPES (X86_PAT_MAX + 1)
+#define X86_PAT_UNSUPPORTED X86_PAT_NUMTYPES
+
+/* This is the x86_memcache_type to PAT index mapping:
+ Note that mappings might be altered during x86_pat_init ().
+*/
+static uint8_t x86_pat_mct2pi[X86_PAT_NUMTYPES] = {
+ [X86_PAT_WB] = 0,
+ [X86_PAT_WT] = 1,
+ [X86_PAT_UCM] = 2,
+ [X86_PAT_UC] = 3,
+ [X86_PAT_WC] = X86_PAT_UNSUPPORTED,
+ [X86_PAT_WP] = X86_PAT_UNSUPPORTED
+};
+
+inline
+static uint64_t
+x86_pat_entry (const uint8_t index,
+ const x86_memcache_type mem_type)
+{
+ return (((uint64_t)mem_type) << (index * 8));
+}
+
+void
+x86_pat_init (void)
+{
+ if (pat_support)
+ return;
+
+ if (CPU_HAS_FEATURE (CPU_FEATURE_PAT))
+ {
+ uint64_t pat;
+
+#ifndef MACH_XEN
+ pat = (x86_pat_entry(0, X86_PAT_WB) |
+ x86_pat_entry(1, X86_PAT_WT) |
+ x86_pat_entry(2, X86_PAT_UCM) |
+ x86_pat_entry(3, X86_PAT_UC) |
+ x86_pat_entry(4, X86_PAT_WC) |
+ x86_pat_entry(5, X86_PAT_WP) |
+ x86_pat_entry(6, X86_PAT_UCM) |
+ x86_pat_entry(7, X86_PAT_UC));
+
+ wrmsr (X86_PAT_MSR, pat);
+ boot_cpu_pat = pat;
+
+#else
+ pat = rdmsr (X86_PAT_MSR);
+#endif
+
+ memset (x86_pat_mct2pi, X86_PAT_UNSUPPORTED, sizeof (x86_pat_mct2pi));
+
+ for (int i = 7; i >= 0; i--)
+ {
+ uint8_t memcache_type = ((pat >> (i * 8)) & 0x7);
+
+ if (memcache_type < X86_PAT_NUMTYPES)
+ x86_pat_mct2pi[memcache_type] = i;
+ }
+
+ /* We guarantee to support certain memory cache types. */
+ if (x86_pat_mct2pi[X86_PAT_UC] == X86_PAT_UNSUPPORTED)
+ panic ("PAT has missing cache types");
+
+ pat_support = 1;
+ }
+}
+
+#define INTEL_PWT_BIT 3
+#define INTEL_PCD_BIT 4
+#define INTEL_PTE_PAT_BIT 7
+
+inline
+static pt_mem_type_t
+x86_pat_index_to_pte(uint8_t index)
+{
+ /* PWT and PCD are adjacent bits. */
+ pt_mem_type_t mem_type = ((index & 0x3) << INTEL_PWT_BIT);
+
+ if (index & 0x4)
+ mem_type |= (1 << INTEL_PTE_PAT_BIT);
+
+ return mem_type;
+}
+
+boolean_t
+x86_pat_memtype_to_pte (const x86_memcache_type mem_type, pt_mem_type_t *pte)
+{
+ if (mem_type >= 0 && mem_type < X86_PAT_NUMTYPES
+ && x86_pat_mct2pi[mem_type] != X86_PAT_UNSUPPORTED)
+ {
+ *pte = x86_pat_index_to_pte (x86_pat_mct2pi[mem_type]);
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+void
+x86_pat_copy_boot_cpu (void)
+{
+#ifndef MACH_XEN
+ if (pat_support && CPU_HAS_FEATURE (CPU_FEATURE_PAT))
+ {
+ wrmsr (X86_PAT_MSR, boot_cpu_pat);
+ }
+#endif
+}
+
+pt_mem_type_t
+x86_pat_uncached (void)
+{
+ return x86_pat_index_to_pte (x86_pat_mct2pi[X86_PAT_UC]);
+}
diff --git a/i386/i386/pat.h b/i386/i386/pat.h
new file mode 100644
index 00000000..83faf253
--- /dev/null
+++ b/i386/i386/pat.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2026 Free Software Foundation, Inc.
+ *
+ * Permission to use, copy, modify and distribute this software and its
+ * documentation is hereby granted, provided that both the copyright
+ * notice and this permission notice appear in all copies of the
+ * software, derivative works or modified versions, and any portions
+ * thereof, and that both notices appear in supporting documentation.
+ *
+ * THE FREE SOFTWARE FOUNDATION ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
+ * CONDITION. THE FREE SOFTWARE FOUNDATION DISCLAIMS ANY LIABILITY OF ANY KIND
+ * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
+ */
+
+#ifndef _I386_PAT_H
+#define _I386_PAT_H
+
+#include <vm/pmap.h>
+
+/* The Page Attribute Table (PAT) allows a 'memory type' (for example
+ Write Combining) to be associated with each page table entry
+ (PTE). The PAT itself is an 8 element table with each entry
+ containing one of the required memory types. The PTE stores the
+ index into this table, that contains the memory type required by
+ that page. An index is therefore a 3 bit value which is encoded in
+ the PTE thus:
+
+ Bit 0: Page Write Though (PWT pte bit 3)
+ Bit 1: Page Cache Disable (PCD pte bit 4)
+ Bit 2: Page Attribute Table (bit 7 for pte and bit 12 for page dir)
+
+ To support processors which pre-date the PAT, those indexes that
+ are encoded using only PWT and PCD are used for memory types
+ appropriate to the original meaning for PWT and PCD. This is the
+ processor default and maintained here in this code:
+
+ PAT0 WB (Write Back)
+ PAT1 WT (Write Through)
+ PAT2 UCM (Uncached - memory type can be overridden by MTRR)
+ PAT3 UC (Uncached)
+
+ This module interface is usable with or without the PAT hardware
+ support. The following memory types are always supported:
+ X86_PAT_UC, X86_PAT_WT, X86_PAT_WB and X86_PAT_UCM. The remaining
+ types are conditional on the PAT hardware and code needs to
+ consider that those types might not be supported.
+*/
+
+#define X86_PAT_MSR (0x277)
+
+/* The values for these enumerations match the INTEL values which are
+ stored in the page attribute table entries. This is a convenience
+ for implementation and should not be relied on.
+*/
+typedef enum
+ {
+ X86_PAT_UC = 0,
+ X86_PAT_WC = 1,
+ X86_PAT_WT = 4,
+ X86_PAT_WP = 5,
+ X86_PAT_WB = 6,
+ X86_PAT_UCM = 7,
+
+ X86_PAT_MAX = 7
+ } x86_memcache_type;
+
+void
+x86_pat_init (void);
+
+/* Return the index into the PAT that should be used for the given
+ memory cache type. Returns FALSE in the case where the system
+ cannot support mem_type. */
+boolean_t
+x86_pat_memtype_to_pte (const x86_memcache_type mem_type, pt_mem_type_t *pte);
+
+/* The PAT configuration needs to be the same across all CPUs.
+ Copy the configuration of the boot CPU into the current one.
+*/
+void
+x86_pat_copy_boot_cpu (void);
+
+/* The type X86_PAT_UC is always supported. */
+pt_mem_type_t
+x86_pat_uncached (void);
+
+#endif /* _I386_PAT_H */
diff --git a/i386/i386at/acpi_parse_apic.c b/i386/i386at/acpi_parse_apic.c
index 58aa6b16..b82a0cea 100644
--- a/i386/i386at/acpi_parse_apic.c
+++ b/i386/i386at/acpi_parse_apic.c
@@ -33,6 +33,7 @@
#include <kern/debug.h>
#include <i386/vm_param.h> /* phystokv */
#include <i386/apic.h> /* lapic, ioapic... */
+#include <i386/pat.h>
#include <i386at/acpi_parse_apic.h>
#include <vm/vm_kern.h>
@@ -262,6 +263,20 @@ acpi_get_sdt_base (int *is_64bit)
return sdt_base;
}
+inline
+static void*
+kmem_map_aligned_table(
+ phys_addr_t phys_address,
+ vm_size_t size,
+ int mode)
+{
+ return kmem_map_aligned_physmem(
+ phys_address,
+ size,
+ x86_pat_uncached(),
+ mode);
+}
+
/*
* acpi_get_rsdt: Get RSDT table reference from RSDP entries.
*
diff --git a/i386/i386at/model_dep.c b/i386/i386at/model_dep.c
index 94186f19..1bca82e0 100644
--- a/i386/i386at/model_dep.c
+++ b/i386/i386at/model_dep.c
@@ -60,6 +60,7 @@
#include <i386/ldt.h>
#include <i386/spl.h>
#include <i386/mp_desc.h>
+#include <i386/pat.h>
#include <i386/pit.h>
#include <i386/pmap.h>
#include <i386/proc_reg.h>
@@ -807,6 +808,9 @@ void c_boot_entry(vm_offset_t bi, uint32_t magic)
#endif
{
const struct multiboot2_raw_info *mb2_info = NULL;
+ int cpu_type = discover_x86_cpu_type ();
+
+ x86_pat_init ();
#if ENABLE_IMMEDIATE_CONSOLE
immc_enable((const void *)phystokv(bi), magic);
@@ -835,8 +839,6 @@ void c_boot_entry(vm_offset_t bi, uint32_t magic)
multiboot1 cases. */
boot_info = *(typeof(boot_info)*)phystokv(bi);
- int cpu_type;
-
/* Before we do _anything_ else, print the hello message.
If there are no initialized console devices yet,
it will be stored and printed at the first opportunity. */
@@ -852,8 +854,6 @@ void c_boot_entry(vm_offset_t bi, uint32_t magic)
#endif
#endif /* MACH_XEN */
- cpu_type = discover_x86_cpu_type ();
-
/*
* Do basic VM initialization
*/
diff --git a/i386/intel/pmap.c b/i386/intel/pmap.c
index c63502ed..4e580fa1 100644
--- a/i386/intel/pmap.c
+++ b/i386/intel/pmap.c
@@ -570,6 +570,7 @@ vm_offset_t pmap_map_bd(
vm_offset_t virt,
phys_addr_t start,
phys_addr_t end,
+ pt_mem_type_t mem_type,
vm_prot_t prot)
{
pt_entry_t template;
@@ -581,7 +582,7 @@ vm_offset_t pmap_map_bd(
#endif /* MACH_PV_PAGETABLES */
template = pa_to_pte(start)
- | INTEL_PTE_NCACHE|INTEL_PTE_WTHRU
+ | mem_type
| INTEL_PTE_VALID;
if (CPU_HAS_FEATURE(CPU_FEATURE_PGE))
template |= INTEL_PTE_GLOBAL;
diff --git a/i386/intel/pmap.h b/i386/intel/pmap.h
index 5c2242f5..0038eb85 100644
--- a/i386/intel/pmap.h
+++ b/i386/intel/pmap.h
@@ -67,6 +67,19 @@
typedef phys_addr_t pt_entry_t;
#define PT_ENTRY_NULL ((pt_entry_t *) 0)
+/* The pt_mem_type_t is formed from these bit fields:
+ PWT bit 3 (INTEL_PTE_WTHRU)
+ PCD bit 4 (INTEL_PTE_NCACHE)
+ PAT bit 7
+
+ More recent processors use these bits in combination to index into
+ the Page Attribute Table (PAT). Processors that pre-date the PAT
+ can still use bits 3 and 4 as a legacy memory type specification.
+ Any values of this type are assumed to be well formed by the
+ caller.
+*/
+typedef pt_entry_t pt_mem_type_t;
+
#endif /* __ASSEMBLER__ */
#define INTEL_OFFMASK 0xfff /* offset within page */
diff --git a/linux/dev/glue/kmem.c b/linux/dev/glue/kmem.c
index afcbfba5..cbf57dd6 100644
--- a/linux/dev/glue/kmem.c
+++ b/linux/dev/glue/kmem.c
@@ -567,6 +567,7 @@ vmtophys (void *addr)
/* XXX: Quick hacking. */
/* Remap physical address into virtual address. */
+#include <i386/pat.h>
#include <vm/pmap.h>
void *
@@ -582,6 +583,7 @@ vremap (unsigned long offset, unsigned long size)
return NULL;
(void) pmap_map_bd (addr, offset, offset + round_page (size),
+ x86_pat_uncached(),
VM_PROT_READ | VM_PROT_WRITE);
vmalloc_list_insert (addr, round_page (size));
diff --git a/vm/pmap.h b/vm/pmap.h
index ef0fe965..8d55d93c 100644
--- a/vm/pmap.h
+++ b/vm/pmap.h
@@ -203,6 +203,7 @@ extern vm_offset_t pmap_map_bd(
vm_offset_t virt,
phys_addr_t start,
phys_addr_t end,
+ pt_mem_type_t mem_type,
vm_prot_t prot);
/*
diff --git a/vm/vm_kern.c b/vm/vm_kern.c
index 07e3b966..50412f2c 100644
--- a/vm/vm_kern.c
+++ b/vm/vm_kern.c
@@ -653,7 +653,7 @@ retry:
}
/*
- * kmem_map_aligned_table: map a table or structure in a virtual memory page
+ * kmem_map_aligned_physmem: map a table or structure in a virtual memory page
* Align the table initial address with the page initial address.
*
* Parameters:
@@ -665,9 +665,10 @@ retry:
*/
void*
-kmem_map_aligned_table(
+kmem_map_aligned_physmem(
phys_addr_t phys_address,
vm_size_t size,
+ pt_mem_type_t mem_type,
int mode)
{
vm_offset_t virt_addr;
@@ -684,7 +685,9 @@ kmem_map_aligned_table(
return NULL;
(void) pmap_map_bd(virt_addr, nearest_page,
- nearest_page + round_page(size), mode);
+ nearest_page + round_page(size),
+ mem_type,
+ mode);
/* XXX remember mapping somewhere so we can free it? */
diff --git a/vm/vm_kern.h b/vm/vm_kern.h
index b9411522..1db7eea2 100644
--- a/vm/vm_kern.h
+++ b/vm/vm_kern.h
@@ -35,6 +35,7 @@
#define _VM_VM_KERN_H_
#include <mach/kern_return.h>
+#include <vm/pmap.h>
#include <vm/vm_map.h>
extern kern_return_t projected_buffer_allocate(vm_map_t, vm_size_t, int,
@@ -56,7 +57,7 @@ extern kern_return_t kmem_valloc(vm_map_t, vm_offset_t *,
vm_size_t);
extern kern_return_t kmem_alloc_wired(vm_map_t, vm_offset_t *, vm_size_t);
extern kern_return_t kmem_alloc_wired_flags(vm_map_t, vm_offset_t *,
vm_size_t, unsigned flags);
extern kern_return_t kmem_alloc_aligned(vm_map_t, vm_offset_t *, vm_size_t);
-extern void* kmem_map_aligned_table(phys_addr_t, vm_size_t, int);
+extern void* kmem_map_aligned_physmem(phys_addr_t, vm_size_t,
pt_mem_type_t, int);
extern void kmem_free(vm_map_t, vm_offset_t, vm_size_t);
diff --git a/x86_64/Makefrag.am b/x86_64/Makefrag.am
index c08a8fdf..0ae8d5b0 100644
--- a/x86_64/Makefrag.am
+++ b/x86_64/Makefrag.am
@@ -28,6 +28,8 @@ if HOST_x86_64
#
libkernel_a_SOURCES += \
+ i386/i386/pat.c \
+ i386/i386/pat.h \
i386/i386at/autoconf.c \
i386/i386at/autoconf.h \
i386/i386at/biosmem.c \
--
2.47.3