KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
mmu.h File Reference

Memory Management Unit and Translation Lookaside Buffer handling. More...

#include <sys/cdefs.h>
#include <stdbool.h>
#include <arch/types.h>
#include <sys/uio.h>

Go to the source code of this file.

Data Structures

struct  mmupage_t
 MMU TLB entry for a single page. More...
 
struct  mmusubcontext_t
 MMU sub-context type. More...
 
struct  mmucontext_t
 MMU context type. More...
 

Macros

#define MMU_TOP_SHIFT   21
 Top-level shift.
 
#define MMU_TOP_BITS   10
 Top-level bits.
 
#define MMU_TOP_MASK   ((1 << MMU_TOP_BITS) - 1)
 Top-level mask.
 
#define MMU_BOT_SHIFT   12
 Bottom shift.
 
#define MMU_BOT_BITS   9
 Bottom bits.
 
#define MMU_BOT_MASK   ((1 << MMU_BOT_BITS) - 1)
 Bottom mask.
 
#define MMU_IND_SHIFT   0
 Index shift.
 
#define MMU_IND_BITS   12
 Index bits.
 
#define MMU_IND_MASK   ((1 << MMU_IND_BITS) - 1)
 Index mask.
 
#define MMU_KERNEL_RDONLY   0
 No user access, kernel read-only.
 
#define MMU_KERNEL_RDWR   1
 No user access, kernel full.
 
#define MMU_ALL_RDONLY   2
 Read-only user and kernel.
 
#define MMU_ALL_RDWR   3
 Full access, user and kernel.
 
#define MMU_NO_CACHE   1
 Cache disabled.
 
#define MMU_CACHE_BACK   2
 Write-back caching.
 
#define MMU_CACHE_WT   3
 Write-through caching.
 
#define MMU_CACHEABLE   MMU_CACHE_BACK
 Default caching.
 
#define MMU_SUB_PAGES   512
 The number of pages in a sub-context.
 
#define MMU_PAGES   1024
 The number of sub-contexts in the main level context.
 

Typedefs

typedef mmupage_t *(* mmu_mapfunc_t) (mmucontext_t *context, int virtpage)
 MMU mapping handler.
 

Functions

void mmu_use_table (mmucontext_t *context)
 Set the "current" page tables for TLB handling.
 
mmucontext_tmmu_context_create (int asid)
 Allocate a new MMU context.
 
void mmu_context_destroy (mmucontext_t *context)
 Destroy an MMU context when a process is being destroyed.
 
int mmu_virt_to_phys (mmucontext_t *context, int virtpage)
 Using the given page tables, translate the virtual page ID to a physical page ID.
 
int mmu_phys_to_virt (mmucontext_t *context, int physpage)
 Using the given page tables, translate the physical page ID to a virtual page ID.
 
void mmu_switch_context (mmucontext_t *context)
 Switch to the given context.
 
void mmu_page_map (mmucontext_t *context, int virtpage, int physpage, int count, int prot, int cache, int share, int dirty)
 Set the given virtual page to map to the given physical page.
 
int mmu_copyin (mmucontext_t *context, uint32 srcaddr, uint32 srccnt, void *buffer)
 Copy a chunk of data from a process' address space into a kernel buffer, taking into account page mappings.
 
int mmu_copyv (mmucontext_t *context1, struct iovec *iov1, int iovcnt1, mmucontext_t *context2, struct iovec *iov2, int iovcnt2)
 Copy a chunk of data from one process' address space to another process' address space, taking into account page mappings.
 
mmu_mapfunc_t mmu_map_get_callback (void)
 Get the current mapping function.
 
mmu_mapfunc_t mmu_map_set_callback (mmu_mapfunc_t newfunc)
 Set a new MMU mapping handler.
 
int mmu_init (void)
 Initialize MMU support.
 
void mmu_shutdown (void)
 Shutdown MMU support.
 
void mmu_reset_itlb (void)
 Reset ITLB.
 
bool mmu_enabled (void)
 Check if MMU translation is enabled.
 
void mmu_set_sq_addr (void *addr)
 Reset the base target address for store queues.
 

Detailed Description

Memory Management Unit and Translation Lookaside Buffer handling.

This file defines the interface to the Memory Management Unit (MMU) in the SH4. The MMU, while not used normally by KOS, is available for virtual memory use, if you so desire. While using this functionality is probably overkill for most homebrew, there are a few very interesting things that this functionality could be used for (like mapping large files into memory that wouldn't otherwise fit).

The whole system is set up as a normal paged memory virtual->physical address translation. KOS implements the page table as a sparse, two-level page table. By default, pages are 4KB in size. Each top-level page table entry has 512 2nd level entries (there are 1024 entries in the top-level entry). This works out to about 2KB of space needed for one top-level entry.

The SH4 itself has 4 TLB entries for instruction fetches, and 64 "unified" TLB entries (for combined instructions + data). Essentially, the UTLB acts both as the TLB for data accesses (from mov instructions) and as a cache for entries for the ITLB. If there is no entry in the ITLB for an instruction access, the UTLB will automatically be searched. If no entry is found still, an ITLB miss exception will be generated. Data accesses are handled similarly to this (although additional complications are involved due to write accesses, and of course the ITLB doesn't play into data accesses).

For more information about how the MMU works, refer to the Hitachi/Renesas SH4 programming manual. It has much more detailed information than what is in here, for obvious reasons.

This functionality was ported over to mainline KOS from the KOS-MMU project of Megan Potter. Unfortunately, KOS-MMU never reached a real phase of maturity and usefulness, but this piece can be quite useful on its own.

Author
Megan Potter

Macro Definition Documentation

◆ MMU_PAGES

#define MMU_PAGES   1024

The number of sub-contexts in the main level context.