Changeset da1bafb in mainline for kernel/genarch/src


Ignore:
Timestamp:
2010-05-24T18:57:31Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0095368
Parents:
666f492
Message:

major code revision

  • replace spinlocks taken with interrupts disabled with irq_spinlocks
  • change spacing (not indendation) to be tab-size independent
  • use unsigned integer types where appropriate (especially bit flags)
  • visual separation
  • remove argument names in function prototypes
  • string changes
  • correct some formating directives
  • replace various cryptic single-character variables (t, a, m, c, b, etc.) with proper identifiers (thread, task, timeout, as, itm, itc, etc.)
  • unify some assembler constructs
  • unused page table levels are now optimized out in compile time
  • replace several ints (with boolean semantics) with bools
  • use specifically sized types instead of generic types where appropriate (size_t, uint32_t, btree_key_t)
  • improve comments
  • split asserts with conjuction into multiple independent asserts
Location:
kernel/genarch/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • kernel/genarch/src/drivers/ega/ega.c

    r666f492 rda1bafb  
    6363
    6464typedef struct {
    65         SPINLOCK_DECLARE(lock);
     65        IRQ_SPINLOCK_DECLARE(lock);
    6666       
    6767        uint32_t cursor;
     
    7171} ega_instance_t;
    7272
    73 static void ega_putchar(outdev_t *dev, wchar_t ch, bool silent);
    74 static void ega_redraw(outdev_t *dev);
     73static void ega_putchar(outdev_t *, wchar_t, bool);
     74static void ega_redraw(outdev_t *);
    7575
    7676static outdev_operations_t egadev_ops = {
     
    540540        ega_instance_t *instance = (ega_instance_t *) dev->data;
    541541       
    542         ipl_t ipl = interrupts_disable();
    543         spinlock_lock(&instance->lock);
     542        irq_spinlock_lock(&instance->lock, true);
    544543       
    545544        switch (ch) {
     
    564563        ega_move_cursor(instance, silent);
    565564       
    566         spinlock_unlock(&instance->lock);
    567         interrupts_restore(ipl);
     565        irq_spinlock_unlock(&instance->lock, true);
    568566}
    569567
     
    572570        ega_instance_t *instance = (ega_instance_t *) dev->data;
    573571       
    574         ipl_t ipl = interrupts_disable();
    575         spinlock_lock(&instance->lock);
     572        irq_spinlock_lock(&instance->lock, true);
    576573       
    577574        memcpy(instance->addr, instance->backbuf, EGA_VRAM_SIZE);
     
    579576        ega_show_cursor(instance, silent);
    580577       
    581         spinlock_unlock(&instance->lock);
    582         interrupts_restore(ipl);
     578        irq_spinlock_unlock(&instance->lock, true);
    583579}
    584580
     
    598594        egadev->data = instance;
    599595       
    600         spinlock_initialize(&instance->lock, "*ega_lock");
     596        irq_spinlock_initialize(&instance->lock, "*ega.instance.lock");
    601597       
    602598        instance->base = base;
  • kernel/genarch/src/mm/as_ht.c

    r666f492 rda1bafb  
    3030 * @{
    3131 */
    32  
     32
    3333/**
    3434 * @file
    35  * @brief       Address space functions for global page hash table.
     35 * @brief Address space functions for global page hash table.
    3636 */
    3737
     
    4646#include <synch/mutex.h>
    4747
    48 static pte_t *ht_create(int flags);
    49 static void ht_destroy(pte_t *page_table);
     48static pte_t *ht_create(unsigned int);
     49static void ht_destroy(pte_t *);
    5050
    51 static void ht_lock(as_t *as, bool lock);
    52 static void ht_unlock(as_t *as, bool unlock);
     51static void ht_lock(as_t *, bool);
     52static void ht_unlock(as_t *, bool);
    5353
    5454as_operations_t as_ht_operations = {
     
    6868 *
    6969 * @return Returns NULL.
     70 *
    7071 */
    71 pte_t *ht_create(int flags)
     72pte_t *ht_create(unsigned int flags)
    7273{
    7374        if (flags & FLAG_AS_KERNEL) {
     
    7576                mutex_initialize(&page_ht_lock, MUTEX_PASSIVE);
    7677        }
     78       
    7779        return NULL;
    7880}
     
    8385 *
    8486 * @param page_table This parameter is ignored.
     87 *
    8588 */
    8689void ht_destroy(pte_t *page_table)
     
    9497 * Interrupts must be disabled.
    9598 *
    96  * @param as Address space.
     99 * @param as   Address space.
    97100 * @param lock If false, do not attempt to lock the address space.
     101 *
    98102 */
    99103void ht_lock(as_t *as, bool lock)
     
    101105        if (lock)
    102106                mutex_lock(&as->lock);
     107       
    103108        mutex_lock(&page_ht_lock);
    104109}
     
    109114 * Interrupts must be disabled.
    110115 *
    111  * @param as Address space.
     116 * @param as     Address space.
    112117 * @param unlock If false, do not attempt to lock the address space.
     118 *
    113119 */
    114120void ht_unlock(as_t *as, bool unlock)
    115121{
    116122        mutex_unlock(&page_ht_lock);
     123       
    117124        if (unlock)
    118125                mutex_unlock(&as->lock);
  • kernel/genarch/src/mm/as_pt.c

    r666f492 rda1bafb  
    3333/**
    3434 * @file
    35  * @brief       Address space functions for 4-level hierarchical pagetables.
     35 * @brief Address space functions for 4-level hierarchical pagetables.
    3636 */
    3737
     
    4747#include <arch.h>
    4848
    49 static pte_t *ptl0_create(int flags);
    50 static void ptl0_destroy(pte_t *page_table);
     49static pte_t *ptl0_create(unsigned int);
     50static void ptl0_destroy(pte_t *);
    5151
    52 static void pt_lock(as_t *as, bool lock);
    53 static void pt_unlock(as_t *as, bool unlock);
     52static void pt_lock(as_t *, bool);
     53static void pt_unlock(as_t *, bool);
    5454
    5555as_operations_t as_pt_operations = {
     
    6767 *
    6868 * @return New PTL0.
     69 *
    6970 */
    70 pte_t *ptl0_create(int flags)
     71pte_t *ptl0_create(unsigned int flags)
    7172{
    72         pte_t *src_ptl0, *dst_ptl0;
    73         ipl_t ipl;
    74         int table_size;
    75 
    76         dst_ptl0 = (pte_t *) frame_alloc(PTL0_SIZE, FRAME_KA);
    77         table_size = FRAME_SIZE << PTL0_SIZE;
    78 
    79         if (flags & FLAG_AS_KERNEL) {
     73        pte_t *dst_ptl0 = (pte_t *) frame_alloc(PTL0_SIZE, FRAME_KA);
     74        size_t table_size = FRAME_SIZE << PTL0_SIZE;
     75       
     76        if (flags & FLAG_AS_KERNEL)
    8077                memsetb(dst_ptl0, table_size, 0);
    81         } else {
    82                 uintptr_t src, dst;
    83        
     78        else {
    8479                /*
    8580                 * Copy the kernel address space portion to new PTL0.
     81                 *
    8682                 */
    87                  
    88                 ipl = interrupts_disable();
    89                 mutex_lock(&AS_KERNEL->lock);           
    90                 src_ptl0 = (pte_t *) PA2KA((uintptr_t) AS_KERNEL->genarch.page_table);
    91 
    92                 src = (uintptr_t) &src_ptl0[PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)];
    93                 dst = (uintptr_t) &dst_ptl0[PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)];
    94 
     83               
     84                ipl_t ipl = interrupts_disable();
     85                mutex_lock(&AS_KERNEL->lock);
     86               
     87                pte_t *src_ptl0 =
     88                    (pte_t *) PA2KA((uintptr_t) AS_KERNEL->genarch.page_table);
     89               
     90                uintptr_t src =
     91                    (uintptr_t) &src_ptl0[PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)];
     92                uintptr_t dst =
     93                    (uintptr_t) &dst_ptl0[PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)];
     94               
    9595                memsetb(dst_ptl0, table_size, 0);
    96                 memcpy((void *) dst, (void *) src, table_size - (src - (uintptr_t) src_ptl0));
     96                memcpy((void *) dst, (void *) src,
     97                    table_size - (src - (uintptr_t) src_ptl0));
     98               
    9799                mutex_unlock(&AS_KERNEL->lock);
    98100                interrupts_restore(ipl);
    99101        }
    100 
     102       
    101103        return (pte_t *) KA2PA((uintptr_t) dst_ptl0);
    102104}
     
    107109 *
    108110 * @param page_table Physical address of PTL0.
     111 *
    109112 */
    110113void ptl0_destroy(pte_t *page_table)
    111114{
    112         frame_free((uintptr_t)page_table);
     115        frame_free((uintptr_t) page_table);
    113116}
    114117
     
    118121 * Interrupts must be disabled.
    119122 *
    120  * @param as Address space.
     123 * @param as   Address space.
    121124 * @param lock If false, do not attempt to lock the address space.
     125 *
    122126 */
    123127void pt_lock(as_t *as, bool lock)
     
    132136 * Interrupts must be disabled.
    133137 *
    134  * @param as Address space.
     138 * @param as     Address space.
    135139 * @param unlock If false, do not attempt to unlock the address space.
     140 *
    136141 */
    137142void pt_unlock(as_t *as, bool unlock)
  • kernel/genarch/src/mm/page_ht.c

    r666f492 rda1bafb  
    3333/**
    3434 * @file
    35  * @brief       Virtual Address Translation (VAT) for global page hash table.
     35 * @brief Virtual Address Translation (VAT) for global page hash table.
    3636 */
    3737
     
    5252#include <align.h>
    5353
    54 static size_t hash(unative_t key[]);
    55 static bool compare(unative_t key[], size_t keys, link_t *item);
    56 static void remove_callback(link_t *item);
    57 
    58 static void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
    59     int flags);
    60 static void ht_mapping_remove(as_t *as, uintptr_t page);
    61 static pte_t *ht_mapping_find(as_t *as, uintptr_t page);
     54static size_t hash(unative_t[]);
     55static bool compare(unative_t[], size_t, link_t *);
     56static void remove_callback(link_t *);
     57
     58static void ht_mapping_insert(as_t *, uintptr_t, uintptr_t, unsigned int);
     59static void ht_mapping_remove(as_t *, uintptr_t);
     60static pte_t *ht_mapping_find(as_t *, uintptr_t);
    6261
    6362/**
     
    6564 * after address space lock and after any address space area
    6665 * locks.
     66 *
    6767 */
    6868mutex_t page_ht_lock;
    6969
    70 /**
    71  * Page hash table.
     70/** Page hash table.
     71 *
    7272 * The page hash table may be accessed only when page_ht_lock is held.
     73 *
    7374 */
    7475hash_table_t page_ht;
     
    9394 *
    9495 * @return Index into page hash table.
     96 *
    9597 */
    9698size_t hash(unative_t key[])
     
    98100        as_t *as = (as_t *) key[KEY_AS];
    99101        uintptr_t page = (uintptr_t) key[KEY_PAGE];
    100         size_t index;
    101102       
    102103        /*
     
    104105         * of occurring. Least significant bits of VPN compose the
    105106         * hash index.
    106          */
    107         index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES - 1));
     107         *
     108         */
     109        size_t index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES - 1));
    108110       
    109111        /*
     
    111113         * similar addresses. Least significant bits compose the
    112114         * hash index.
     115         *
    113116         */
    114117        index |= ((unative_t) as) & (PAGE_HT_ENTRIES - 1);
     
    119122/** Compare page hash table item with page and/or address space.
    120123 *
    121  * @param key Array of one or two keys (i.e. page and/or address space).
     124 * @param key  Array of one or two keys (i.e. page and/or address space).
    122125 * @param keys Number of keys passed.
    123126 * @param item Item to compare the keys with.
    124127 *
    125128 * @return true on match, false otherwise.
     129 *
    126130 */
    127131bool compare(unative_t key[], size_t keys, link_t *item)
    128132{
    129         pte_t *t;
    130 
    131133        ASSERT(item);
    132         ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS));
    133 
     134        ASSERT(keys > 0);
     135        ASSERT(keys <= PAGE_HT_KEYS);
     136       
    134137        /*
    135138         * Convert item to PTE.
    136          */
    137         t = hash_table_get_instance(item, pte_t, link);
    138 
    139         if (keys == PAGE_HT_KEYS) {
    140                 return (key[KEY_AS] == (uintptr_t) t->as) &&
    141                     (key[KEY_PAGE] == t->page);
    142         } else {
    143                 return (key[KEY_AS] == (uintptr_t) t->as);
    144         }
     139         *
     140         */
     141        pte_t *pte = hash_table_get_instance(item, pte_t, link);
     142       
     143        if (keys == PAGE_HT_KEYS)
     144                return (key[KEY_AS] == (uintptr_t) pte->as) &&
     145                    (key[KEY_PAGE] == pte->page);
     146       
     147        return (key[KEY_AS] == (uintptr_t) pte->as);
    145148}
    146149
     
    148151 *
    149152 * @param item Page hash table item being removed.
     153 *
    150154 */
    151155void remove_callback(link_t *item)
    152156{
    153         pte_t *t;
    154 
    155157        ASSERT(item);
    156 
     158       
    157159        /*
    158160         * Convert item to PTE.
    159          */
    160         t = hash_table_get_instance(item, pte_t, link);
    161 
    162         free(t);
     161         *
     162         */
     163        pte_t *pte = hash_table_get_instance(item, pte_t, link);
     164       
     165        free(pte);
    163166}
    164167
     
    166169 *
    167170 * Map virtual address page to physical address frame
    168  * using flags. 
     171 * using flags.
    169172 *
    170173 * The page table must be locked and interrupts must be disabled.
    171174 *
    172  * @param as Address space to which page belongs.
    173  * @param page Virtual address of the page to be mapped.
     175 * @param as    Address space to which page belongs.
     176 * @param page  Virtual address of the page to be mapped.
    174177 * @param frame Physical address of memory frame to which the mapping is done.
    175178 * @param flags Flags to be used for mapping.
    176  */
    177 void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags)
    178 {
    179         pte_t *t;
     179 *
     180 */
     181void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
     182    unsigned int flags)
     183{
    180184        unative_t key[2] = {
    181185                (uintptr_t) as,
     
    184188       
    185189        if (!hash_table_find(&page_ht, key)) {
    186                 t = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC);
    187                 ASSERT(t != NULL);
    188 
    189                 t->g = (flags & PAGE_GLOBAL) != 0;
    190                 t->x = (flags & PAGE_EXEC) != 0;
    191                 t->w = (flags & PAGE_WRITE) != 0;
    192                 t->k = !(flags & PAGE_USER);
    193                 t->c = (flags & PAGE_CACHEABLE) != 0;
    194                 t->p = !(flags & PAGE_NOT_PRESENT);
    195                 t->a = false;
    196                 t->d = false;
    197 
    198                 t->as = as;
    199                 t->page = ALIGN_DOWN(page, PAGE_SIZE);
    200                 t->frame = ALIGN_DOWN(frame, FRAME_SIZE);
    201 
    202                 hash_table_insert(&page_ht, key, &t->link);
     190                pte_t *pte = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC);
     191                ASSERT(pte != NULL);
     192               
     193                pte->g = (flags & PAGE_GLOBAL) != 0;
     194                pte->x = (flags & PAGE_EXEC) != 0;
     195                pte->w = (flags & PAGE_WRITE) != 0;
     196                pte->k = !(flags & PAGE_USER);
     197                pte->c = (flags & PAGE_CACHEABLE) != 0;
     198                pte->p = !(flags & PAGE_NOT_PRESENT);
     199                pte->a = false;
     200                pte->d = false;
     201               
     202                pte->as = as;
     203                pte->page = ALIGN_DOWN(page, PAGE_SIZE);
     204                pte->frame = ALIGN_DOWN(frame, FRAME_SIZE);
     205               
     206                hash_table_insert(&page_ht, key, &pte->link);
    203207        }
    204208}
     
    212216 * The page table must be locked and interrupts must be disabled.
    213217 *
    214  * @param as Address space to wich page belongs.
     218 * @param as   Address space to wich page belongs.
    215219 * @param page Virtual address of the page to be demapped.
     220 *
    216221 */
    217222void ht_mapping_remove(as_t *as, uintptr_t page)
     
    236241 * The page table must be locked and interrupts must be disabled.
    237242 *
    238  * @param as Address space to wich page belongs.
     243 * @param as   Address space to wich page belongs.
    239244 * @param page Virtual page.
    240245 *
    241246 * @return NULL if there is no such mapping; requested mapping otherwise.
     247 *
    242248 */
    243249pte_t *ht_mapping_find(as_t *as, uintptr_t page)
    244250{
    245         link_t *hlp;
    246         pte_t *t = NULL;
    247251        unative_t key[2] = {
    248252                (uintptr_t) as,
     
    250254        };
    251255       
    252         hlp = hash_table_find(&page_ht, key);
    253         if (hlp)
    254                 t = hash_table_get_instance(hlp, pte_t, link);
    255 
    256         return t;
     256        link_t *cur = hash_table_find(&page_ht, key);
     257        if (cur)
     258                return hash_table_get_instance(cur, pte_t, link);
     259       
     260        return NULL;
    257261}
    258262
  • kernel/genarch/src/mm/page_pt.c

    r666f492 rda1bafb  
    3333/**
    3434 * @file
    35  * @brief       Virtual Address Translation for hierarchical 4-level page tables.
     35 * @brief Virtual Address Translation for hierarchical 4-level page tables.
    3636 */
    3737
     
    4646#include <memstr.h>
    4747
    48 static void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags);
    49 static void pt_mapping_remove(as_t *as, uintptr_t page);
    50 static pte_t *pt_mapping_find(as_t *as, uintptr_t page);
     48static void pt_mapping_insert(as_t *, uintptr_t, uintptr_t, unsigned int);
     49static void pt_mapping_remove(as_t *, uintptr_t);
     50static pte_t *pt_mapping_find(as_t *, uintptr_t);
    5151
    5252page_mapping_operations_t pt_mapping_operations = {
     
    6363 * The page table must be locked and interrupts must be disabled.
    6464 *
    65  * @param as Address space to wich page belongs.
    66  * @param page Virtual address of the page to be mapped.
     65 * @param as    Address space to wich page belongs.
     66 * @param page  Virtual address of the page to be mapped.
    6767 * @param frame Physical address of memory frame to which the mapping is done.
    6868 * @param flags Flags to be used for mapping.
    69  */
    70 void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags)
     69 *
     70 */
     71void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
     72    unsigned int flags)
    7173{
    72         pte_t *ptl0, *ptl1, *ptl2, *ptl3;
    73         pte_t *newpt;
    74 
    75         ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
    76 
     74        pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
     75       
    7776        if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) {
    78                 newpt = (pte_t *)frame_alloc(PTL1_SIZE, FRAME_KA);
     77                pte_t *newpt = (pte_t *) frame_alloc(PTL1_SIZE, FRAME_KA);
    7978                memsetb(newpt, FRAME_SIZE << PTL1_SIZE, 0);
    8079                SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt));
    8180                SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
    8281        }
    83 
    84         ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
    85 
     82       
     83        pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
     84       
    8685        if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) {
    87                 newpt = (pte_t *)frame_alloc(PTL2_SIZE, FRAME_KA);
     86                pte_t *newpt = (pte_t *) frame_alloc(PTL2_SIZE, FRAME_KA);
    8887                memsetb(newpt, FRAME_SIZE << PTL2_SIZE, 0);
    8988                SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt));
    9089                SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
    9190        }
    92 
    93         ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
    94 
     91       
     92        pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
     93       
    9594        if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) {
    96                 newpt = (pte_t *)frame_alloc(PTL3_SIZE, FRAME_KA);
     95                pte_t *newpt = (pte_t *) frame_alloc(PTL3_SIZE, FRAME_KA);
    9796                memsetb(newpt, FRAME_SIZE << PTL3_SIZE, 0);
    9897                SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt));
    9998                SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
    10099        }
    101 
    102         ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
    103 
     100       
     101        pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
     102       
    104103        SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(page), frame);
    105104        SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), flags);
     
    116115 * The page table must be locked and interrupts must be disabled.
    117116 *
    118  * @param as Address space to wich page belongs.
     117 * @param as   Address space to wich page belongs.
    119118 * @param page Virtual address of the page to be demapped.
     119 *
    120120 */
    121121void pt_mapping_remove(as_t *as, uintptr_t page)
    122122{
    123         pte_t *ptl0, *ptl1, *ptl2, *ptl3;
    124         bool empty = true;
    125         int i;
    126 
    127123        /*
    128124         * First, remove the mapping, if it exists.
     125         *
    129126         */
    130 
    131         ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
    132 
     127       
     128        pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
    133129        if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
    134130                return;
    135 
    136         ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
    137 
     131       
     132        pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
    138133        if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
    139134                return;
    140 
    141         ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
    142 
     135       
     136        pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
    143137        if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
    144138                return;
    145 
    146         ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
    147 
     139       
     140        pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
     141       
    148142        /* Destroy the mapping. Setting to PAGE_NOT_PRESENT is not sufficient. */
    149143        memsetb(&ptl3[PTL3_INDEX(page)], sizeof(pte_t), 0);
    150 
     144       
    151145        /*
    152146         * Second, free all empty tables along the way from PTL3 down to PTL0.
     147         *
    153148         */
    154149       
    155         /* check PTL3 */
     150        /* Check PTL3 */
     151        bool empty = true;
     152       
     153        unsigned int i;
    156154        for (i = 0; i < PTL3_ENTRIES; i++) {
    157155                if (PTE_VALID(&ptl3[i])) {
     
    160158                }
    161159        }
     160       
    162161        if (empty) {
    163162                /*
    164163                 * PTL3 is empty.
    165164                 * Release the frame and remove PTL3 pointer from preceding table.
     165                 *
    166166                 */
    167167                frame_free(KA2PA((uintptr_t) ptl3));
    168                 if (PTL2_ENTRIES)
    169                         memsetb(&ptl2[PTL2_INDEX(page)], sizeof(pte_t), 0);
    170                 else if (PTL1_ENTRIES)
    171                         memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
    172                 else
    173                         memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
     168#if (PTL2_ENTRIES != 0)
     169                memsetb(&ptl2[PTL2_INDEX(page)], sizeof(pte_t), 0);
     170#elif (PTL1_ENTRIES != 0)
     171                memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
     172#else
     173                memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
     174#endif
    174175        } else {
    175176                /*
     
    177178                 * Therefore, there must be a path from PTL0 to PTL3 and
    178179                 * thus nothing to free in higher levels.
    179                  */
    180                 return;
    181         }
    182        
    183         /* check PTL2, empty is still true */
    184         if (PTL2_ENTRIES) {
    185                 for (i = 0; i < PTL2_ENTRIES; i++) {
    186                         if (PTE_VALID(&ptl2[i])) {
    187                                 empty = false;
    188                                 break;
    189                         }
     180                 *
     181                 */
     182                return;
     183        }
     184       
     185        /* Check PTL2, empty is still true */
     186#if (PTL2_ENTRIES != 0)
     187        for (i = 0; i < PTL2_ENTRIES; i++) {
     188                if (PTE_VALID(&ptl2[i])) {
     189                        empty = false;
     190                        break;
    190191                }
    191                 if (empty) {
    192                         /*
    193                          * PTL2 is empty.
    194                          * Release the frame and remove PTL2 pointer from preceding table.
    195                          */
    196                         frame_free(KA2PA((uintptr_t) ptl2));
    197                         if (PTL1_ENTRIES)
    198                                 memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
    199                         else
    200                                 memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
     192        }
     193       
     194        if (empty) {
     195                /*
     196                 * PTL2 is empty.
     197                 * Release the frame and remove PTL2 pointer from preceding table.
     198                 *
     199                 */
     200                frame_free(KA2PA((uintptr_t) ptl2));
     201#if (PTL1_ENTRIES != 0)
     202                memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
     203#else
     204                memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
     205#endif
     206        } else {
     207                /*
     208                 * PTL2 is not empty.
     209                 * Therefore, there must be a path from PTL0 to PTL2 and
     210                 * thus nothing to free in higher levels.
     211                 *
     212                 */
     213                return;
     214        }
     215#endif /* PTL2_ENTRIES != 0 */
     216       
     217        /* check PTL1, empty is still true */
     218#if (PTL1_ENTRIES != 0)
     219        for (i = 0; i < PTL1_ENTRIES; i++) {
     220                if (PTE_VALID(&ptl1[i])) {
     221                        empty = false;
     222                        break;
    201223                }
    202                 else {
    203                         /*
    204                          * PTL2 is not empty.
    205                          * Therefore, there must be a path from PTL0 to PTL2 and
    206                          * thus nothing to free in higher levels.
    207                          */
    208                         return;
    209                 }
    210         }
    211 
    212         /* check PTL1, empty is still true */
    213         if (PTL1_ENTRIES) {
    214                 for (i = 0; i < PTL1_ENTRIES; i++) {
    215                         if (PTE_VALID(&ptl1[i])) {
    216                                 empty = false;
    217                                 break;
    218                         }
    219                 }
    220                 if (empty) {
    221                         /*
    222                          * PTL1 is empty.
    223                          * Release the frame and remove PTL1 pointer from preceding table.
    224                          */
    225                         frame_free(KA2PA((uintptr_t) ptl1));
    226                         memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
    227                 }
    228         }
    229 
     224        }
     225       
     226        if (empty) {
     227                /*
     228                 * PTL1 is empty.
     229                 * Release the frame and remove PTL1 pointer from preceding table.
     230                 *
     231                 */
     232                frame_free(KA2PA((uintptr_t) ptl1));
     233                memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
     234        }
     235#endif /* PTL1_ENTRIES != 0 */
    230236}
    231237
     
    236242 * The page table must be locked and interrupts must be disabled.
    237243 *
    238  * @param as Address space to which page belongs.
     244 * @param as   Address space to which page belongs.
    239245 * @param page Virtual page.
    240246 *
    241  * @return NULL if there is no such mapping; entry from PTL3 describing the mapping otherwise.
     247 * @return NULL if there is no such mapping; entry from PTL3 describing
     248 *         the mapping otherwise.
     249 *
    242250 */
    243251pte_t *pt_mapping_find(as_t *as, uintptr_t page)
    244252{
    245         pte_t *ptl0, *ptl1, *ptl2, *ptl3;
    246 
    247         ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
    248 
     253        pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
    249254        if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
    250255                return NULL;
    251 
    252         ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
    253 
     256       
     257        pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
    254258        if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
    255259                return NULL;
    256 
    257         ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
    258 
     260       
     261        pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
    259262        if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
    260263                return NULL;
    261 
    262         ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
    263 
     264       
     265        pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
     266       
    264267        return &ptl3[PTL3_INDEX(page)];
    265268}
Note: See TracChangeset for help on using the changeset viewer.