Changeset dd0c8a0 in mainline for kernel/generic/src


Ignore:
Timestamp:
2013-09-29T06:56:33Z (12 years ago)
Author:
Beniamino Galvani <b.galvani@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a9bd960d
Parents:
3deb0155 (diff), 13be2583 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

Location:
kernel/generic/src
Files:
1 deleted
30 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/adt/bitmap.c

    r3deb0155 rdd0c8a0  
    3535 *
    3636 * This file implements bitmap ADT and provides functions for
    37  * setting and clearing ranges of bits.
     37 * setting and clearing ranges of bits and for finding ranges
     38 * of unset bits.
    3839 */
    3940
     
    4445#include <macros.h>
    4546
    46 #define ALL_ONES        0xff
    47 #define ALL_ZEROES      0x00
     47#define ALL_ONES    0xff
     48#define ALL_ZEROES  0x00
     49
     50/** Unchecked version of bitmap_get()
     51 *
     52 * This version of bitmap_get() does not do any boundary checks.
     53 *
     54 * @param bitmap  Bitmap to access.
     55 * @param element Element to access.
     56 *
     57 * @return Bit value of the element in the bitmap.
     58 *
     59 */
     60static unsigned int bitmap_get_fast(bitmap_t *bitmap, size_t element)
     61{
     62        size_t byte = element / BITMAP_ELEMENT;
     63        uint8_t mask = 1 << (element & BITMAP_REMAINER);
     64       
     65        return !!((bitmap->bits)[byte] & mask);
     66}
     67
     68/** Get bitmap size
     69 *
     70 * Return the size (in bytes) required for the bitmap.
     71 *
     72 * @param elements   Number bits stored in bitmap.
     73 *
     74 * @return Size (in bytes) required for the bitmap.
     75 *
     76 */
     77size_t bitmap_size(size_t elements)
     78{
     79        size_t size = elements / BITMAP_ELEMENT;
     80       
     81        if ((elements % BITMAP_ELEMENT) != 0)
     82                size++;
     83       
     84        return size;
     85}
    4886
    4987/** Initialize bitmap.
     
    5189 * No portion of the bitmap is set or cleared by this function.
    5290 *
    53  * @param bitmap        Bitmap structure.
    54  * @param map           Address of the memory used to hold the map.
    55  * @param bits          Number of bits stored in bitmap.
    56  */
    57 void bitmap_initialize(bitmap_t *bitmap, uint8_t *map, size_t bits)
    58 {
    59         bitmap->map = map;
    60         bitmap->bits = bits;
     91 * @param bitmap     Bitmap structure.
     92 * @param elements   Number of bits stored in bitmap.
     93 * @param data       Address of the memory used to hold the map.
     94 *                   The optional 2nd level bitmap follows the 1st
     95 *                   level bitmap.
     96 *
     97 */
     98void bitmap_initialize(bitmap_t *bitmap, size_t elements, void *data)
     99{
     100        bitmap->elements = elements;
     101        bitmap->bits = (uint8_t *) data;
     102        bitmap->next_fit = 0;
    61103}
    62104
    63105/** Set range of bits.
    64106 *
    65  * @param bitmap        Bitmap structure.
    66  * @param start         Starting bit.
    67  * @param bits          Number of bits to set.
    68  */
    69 void bitmap_set_range(bitmap_t *bitmap, size_t start, size_t bits)
    70 {
    71         size_t i = 0;
    72         size_t aligned_start;
    73         size_t lub;     /* leading unaligned bits */
    74         size_t amb;     /* aligned middle bits */
    75         size_t tab;     /* trailing aligned bits */
    76        
    77         ASSERT(start + bits <= bitmap->bits);
    78        
    79         aligned_start = ALIGN_UP(start, 8);
    80         lub = min(aligned_start - start, bits);
    81         amb = bits > lub ? bits - lub : 0;
    82         tab = amb % 8;
    83        
    84         if (!bits)
     107 * @param bitmap Bitmap structure.
     108 * @param start  Starting bit.
     109 * @param count  Number of bits to set.
     110 *
     111 */
     112void bitmap_set_range(bitmap_t *bitmap, size_t start, size_t count)
     113{
     114        ASSERT(start + count <= bitmap->elements);
     115       
     116        if (count == 0)
    85117                return;
    86 
    87         if (start + bits < aligned_start) {
     118       
     119        size_t start_byte = start / BITMAP_ELEMENT;
     120        size_t aligned_start = ALIGN_UP(start, BITMAP_ELEMENT);
     121       
     122        /* Leading unaligned bits */
     123        size_t lub = min(aligned_start - start, count);
     124       
     125        /* Aligned middle bits */
     126        size_t amb = (count > lub) ? (count - lub) : 0;
     127       
     128        /* Trailing aligned bits */
     129        size_t tab = amb % BITMAP_ELEMENT;
     130       
     131        if (start + count < aligned_start) {
    88132                /* Set bits in the middle of byte. */
    89                 bitmap->map[start / 8] |= ((1 << lub) - 1) << (start & 7);
     133                bitmap->bits[start_byte] |=
     134                    ((1 << lub) - 1) << (start & BITMAP_REMAINER);
    90135                return;
    91136        }
     
    93138        if (lub) {
    94139                /* Make sure to set any leading unaligned bits. */
    95                 bitmap->map[start / 8] |= ~((1 << (8 - lub)) - 1);
    96         }
    97         for (i = 0; i < amb / 8; i++) {
     140                bitmap->bits[start_byte] |=
     141                    ~((1 << (BITMAP_ELEMENT - lub)) - 1);
     142        }
     143       
     144        size_t i;
     145       
     146        for (i = 0; i < amb / BITMAP_ELEMENT; i++) {
    98147                /* The middle bits can be set byte by byte. */
    99                 bitmap->map[aligned_start / 8 + i] = ALL_ONES;
    100         }
     148                bitmap->bits[aligned_start / BITMAP_ELEMENT + i] =
     149                    ALL_ONES;
     150        }
     151       
    101152        if (tab) {
    102153                /* Make sure to set any trailing aligned bits. */
    103                 bitmap->map[aligned_start / 8 + i] |= (1 << tab) - 1;
    104         }
    105        
     154                bitmap->bits[aligned_start / BITMAP_ELEMENT + i] |=
     155                    (1 << tab) - 1;
     156        }
    106157}
    107158
    108159/** Clear range of bits.
    109160 *
    110  * @param bitmap        Bitmap structure.
    111  * @param start         Starting bit.
    112  * @param bits          Number of bits to clear.
    113  */
    114 void bitmap_clear_range(bitmap_t *bitmap, size_t start, size_t bits)
    115 {
    116         size_t i = 0;
    117         size_t aligned_start;
    118         size_t lub;     /* leading unaligned bits */
    119         size_t amb;     /* aligned middle bits */
    120         size_t tab;     /* trailing aligned bits */
    121        
    122         ASSERT(start + bits <= bitmap->bits);
    123        
    124         aligned_start = ALIGN_UP(start, 8);
    125         lub = min(aligned_start - start, bits);
    126         amb = bits > lub ? bits - lub : 0;
    127         tab = amb % 8;
    128 
    129         if (!bits)
     161 * @param bitmap Bitmap structure.
     162 * @param start  Starting bit.
     163 * @param count  Number of bits to clear.
     164 *
     165 */
     166void bitmap_clear_range(bitmap_t *bitmap, size_t start, size_t count)
     167{
     168        ASSERT(start + count <= bitmap->elements);
     169       
     170        if (count == 0)
    130171                return;
    131 
    132         if (start + bits < aligned_start) {
     172       
     173        size_t start_byte = start / BITMAP_ELEMENT;
     174        size_t aligned_start = ALIGN_UP(start, BITMAP_ELEMENT);
     175       
     176        /* Leading unaligned bits */
     177        size_t lub = min(aligned_start - start, count);
     178       
     179        /* Aligned middle bits */
     180        size_t amb = (count > lub) ? (count - lub) : 0;
     181       
     182        /* Trailing aligned bits */
     183        size_t tab = amb % BITMAP_ELEMENT;
     184       
     185        if (start + count < aligned_start) {
    133186                /* Set bits in the middle of byte */
    134                 bitmap->map[start / 8] &= ~(((1 << lub) - 1) << (start & 7));
     187                bitmap->bits[start_byte] &=
     188                    ~(((1 << lub) - 1) << (start & BITMAP_REMAINER));
    135189                return;
    136190        }
    137 
     191       
    138192        if (lub) {
    139193                /* Make sure to clear any leading unaligned bits. */
    140                 bitmap->map[start / 8] &= (1 << (8 - lub)) - 1;
    141         }
    142         for (i = 0; i < amb / 8; i++) {
     194                bitmap->bits[start_byte] &=
     195                    (1 << (BITMAP_ELEMENT - lub)) - 1;
     196        }
     197       
     198        size_t i;
     199       
     200        for (i = 0; i < amb / BITMAP_ELEMENT; i++) {
    143201                /* The middle bits can be cleared byte by byte. */
    144                 bitmap->map[aligned_start / 8 + i] = ALL_ZEROES;
    145         }
     202                bitmap->bits[aligned_start / BITMAP_ELEMENT + i] =
     203                    ALL_ZEROES;
     204        }
     205       
    146206        if (tab) {
    147207                /* Make sure to clear any trailing aligned bits. */
    148                 bitmap->map[aligned_start / 8 + i] &= ~((1 << tab) - 1);
    149         }
    150 
     208                bitmap->bits[aligned_start / BITMAP_ELEMENT + i] &=
     209                    ~((1 << tab) - 1);
     210        }
     211       
     212        bitmap->next_fit = start_byte;
    151213}
    152214
    153215/** Copy portion of one bitmap into another bitmap.
    154216 *
    155  * @param dst           Destination bitmap.
    156  * @param src           Source bitmap.
    157  * @param bits          Number of bits to copy.
    158  */
    159 void bitmap_copy(bitmap_t *dst, bitmap_t *src, size_t bits)
    160 {
     217 * @param dst   Destination bitmap.
     218 * @param src   Source bitmap.
     219 * @param count Number of bits to copy.
     220 *
     221 */
     222void bitmap_copy(bitmap_t *dst, bitmap_t *src, size_t count)
     223{
     224        ASSERT(count <= dst->elements);
     225        ASSERT(count <= src->elements);
     226       
    161227        size_t i;
    162228       
    163         ASSERT(bits <= dst->bits);
    164         ASSERT(bits <= src->bits);
    165        
    166         for (i = 0; i < bits / 8; i++)
    167                 dst->map[i] = src->map[i];
    168        
    169         if (bits % 8) {
    170                 bitmap_clear_range(dst, i * 8, bits % 8);
    171                 dst->map[i] |= src->map[i] & ((1 << (bits % 8)) - 1);
    172         }
     229        for (i = 0; i < count / BITMAP_ELEMENT; i++)
     230                dst->bits[i] = src->bits[i];
     231       
     232        if (count % BITMAP_ELEMENT) {
     233                bitmap_clear_range(dst, i * BITMAP_ELEMENT,
     234                    count % BITMAP_ELEMENT);
     235                dst->bits[i] |= src->bits[i] &
     236                    ((1 << (count % BITMAP_ELEMENT)) - 1);
     237        }
     238}
     239
     240static int constraint_satisfy(size_t index, size_t base, size_t constraint)
     241{
     242        return (((base + index) & constraint) == 0);
     243}
     244
     245/** Find a continuous zero bit range
     246 *
     247 * Find a continuous zero bit range in the bitmap. The address
     248 * computed as the sum of the index of the first zero bit and
     249 * the base argument needs to be compliant with the constraint
     250 * (those bits that are set in the constraint cannot be set in
     251 * the address).
     252 *
     253 * If the index argument is non-NULL, the continuous zero range
     254 * is set and the index of the first bit is stored to index.
     255 * Otherwise the bitmap stays untouched.
     256 *
     257 * @param bitmap     Bitmap structure.
     258 * @param count      Number of continuous zero bits to find.
     259 * @param base       Address of the first bit in the bitmap.
     260 * @param prefered   Prefered address to start searching from.
     261 * @param constraint Constraint for the address of the first zero bit.
     262 * @param index      Place to store the index of the first zero
     263 *                   bit. Can be NULL (in which case the bitmap
     264 *                   is not modified).
     265 *
     266 * @return Non-zero if a continuous range of zero bits satisfying
     267 *         the constraint has been found.
     268 * @return Zero otherwise.
     269 *
     270 */
     271int bitmap_allocate_range(bitmap_t *bitmap, size_t count, size_t base,
     272    size_t prefered, size_t constraint, size_t *index)
     273{
     274        if (count == 0)
     275                return false;
     276       
     277        size_t size = bitmap_size(bitmap->elements);
     278        size_t next_fit = bitmap->next_fit;
     279       
     280        /*
     281         * Adjust the next-fit value according to the address
     282         * the caller prefers to start the search at.
     283         */
     284        if ((prefered > base) && (prefered < base + bitmap->elements)) {
     285                size_t prefered_fit = (prefered - base) / BITMAP_ELEMENT;
     286               
     287                if (prefered_fit > next_fit)
     288                        next_fit = prefered_fit;
     289        }
     290       
     291        for (size_t pos = 0; pos < size; pos++) {
     292                size_t byte = (next_fit + pos) % size;
     293               
     294                /* Skip if the current byte has all bits set */
     295                if (bitmap->bits[byte] == ALL_ONES)
     296                        continue;
     297               
     298                size_t byte_bit = byte * BITMAP_ELEMENT;
     299               
     300                for (size_t bit = 0; bit < BITMAP_ELEMENT; bit++) {
     301                        size_t i = byte_bit + bit;
     302                       
     303                        if (i >= bitmap->elements)
     304                                break;
     305                       
     306                        if (!constraint_satisfy(i, base, constraint))
     307                                continue;
     308                       
     309                        if (!bitmap_get_fast(bitmap, i)) {
     310                                size_t continuous = 1;
     311                               
     312                                for (size_t j = 1; j < count; j++) {
     313                                        if ((i + j < bitmap->elements) &&
     314                                            (!bitmap_get_fast(bitmap, i + j)))
     315                                                continuous++;
     316                                        else
     317                                                break;
     318                                }
     319                               
     320                                if (continuous == count) {
     321                                        if (index != NULL) {
     322                                                bitmap_set_range(bitmap, i, count);
     323                                                bitmap->next_fit = i / BITMAP_ELEMENT;
     324                                                *index = i;
     325                                        }
     326                                       
     327                                        return true;
     328                                } else
     329                                        i += continuous;
     330                        }
     331                }
     332        }
     333       
     334        return false;
    173335}
    174336
  • kernel/generic/src/adt/btree.c

    r3deb0155 rdd0c8a0  
    10311031       
    10321032        printf("Printing list of leaves:\n");
    1033         list_foreach(t->leaf_list, cur) {
    1034                 btree_node_t *node;
    1035                
    1036                 node = list_get_instance(cur, btree_node_t, leaf_link);
    1037                
     1033        list_foreach(t->leaf_list, leaf_link, btree_node_t, node) {
    10381034                ASSERT(node);
    10391035               
  • kernel/generic/src/adt/hash_table.c

    r3deb0155 rdd0c8a0  
    117117        ASSERT(chain < h->entries);
    118118       
    119         list_foreach(h->entry[chain], cur) {
     119        link_t *cur = list_first(&h->entry[chain]);
     120        while (cur != NULL) {
    120121                if (h->op->compare(key, h->max_keys, cur)) {
    121122                        /*
     
    124125                        return cur;
    125126                }
     127                cur = list_next(cur, &h->entry[chain]);
    126128        }
    127129       
  • kernel/generic/src/adt/list.c

    r3deb0155 rdd0c8a0  
    101101        unsigned int count = 0;
    102102       
    103         list_foreach(*list, link) {
     103        link_t *link = list_first(list);
     104        while (link != NULL) {
    104105                count++;
     106                link = list_next(link, list);
    105107        }
    106108       
  • kernel/generic/src/console/cmd.c

    r3deb0155 rdd0c8a0  
    656656       
    657657        size_t len = 0;
    658         list_foreach(cmd_list, cur) {
    659                 cmd_info_t *hlp;
    660                 hlp = list_get_instance(cur, cmd_info_t, link);
    661                
     658        list_foreach(cmd_list, link, cmd_info_t, hlp) {
    662659                spinlock_lock(&hlp->lock);
    663660                if (str_length(hlp->name) > len)
     
    672669        }
    673670       
    674         list_foreach(cmd_list, cur) {
    675                 cmd_info_t *hlp;
    676                 hlp = list_get_instance(cur, cmd_info_t, link);
    677                
     671        list_foreach(cmd_list, link, cmd_info_t, hlp) {
    678672                spinlock_lock(&hlp->lock);
    679673                printf("%-*s %s\n", _len, hlp->name, hlp->description);
     
    912906        spinlock_lock(&cmd_lock);
    913907       
    914         list_foreach(cmd_list, cur) {
    915                 cmd_info_t *hlp;
    916                
    917                 hlp = list_get_instance(cur, cmd_info_t, link);
     908        list_foreach(cmd_list, link, cmd_info_t, hlp) {
    918909                spinlock_lock(&hlp->lock);
    919910               
  • kernel/generic/src/console/console.c

    r3deb0155 rdd0c8a0  
    125125static void stdout_write(outdev_t *dev, wchar_t ch)
    126126{
    127         list_foreach(dev->list, cur) {
    128                 outdev_t *sink = list_get_instance(cur, outdev_t, link);
     127        list_foreach(dev->list, link, outdev_t, sink) {
    129128                if ((sink) && (sink->op->write))
    130129                        sink->op->write(sink, ch);
     
    134133static void stdout_redraw(outdev_t *dev)
    135134{
    136         list_foreach(dev->list, cur) {
    137                 outdev_t *sink = list_get_instance(cur, outdev_t, link);
     135        list_foreach(dev->list, link, outdev_t, sink) {
    138136                if ((sink) && (sink->op->redraw))
    139137                        sink->op->redraw(sink);
  • kernel/generic/src/console/kconsole.c

    r3deb0155 rdd0c8a0  
    5353#include <func.h>
    5454#include <str.h>
    55 #include <macros.h>
    5655#include <sysinfo/sysinfo.h>
    5756#include <ddi/device.h>
     
    119118         * Make sure the command is not already listed.
    120119         */
    121         list_foreach(cmd_list, cur) {
    122                 cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
    123                
     120        list_foreach(cmd_list, link, cmd_info_t, hlp) {
    124121                if (hlp == cmd) {
    125122                        /* The command is already there. */
     
    613610        cmd_info_t *cmd = NULL;
    614611       
    615         list_foreach(cmd_list, cur) {
    616                 cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
     612        list_foreach(cmd_list, link, cmd_info_t, hlp) {
    617613                spinlock_lock(&hlp->lock);
    618614               
  • kernel/generic/src/cpu/cpu.c

    r3deb0155 rdd0c8a0  
    7373                size_t i;
    7474                for (i = 0; i < config.cpu_count; i++) {
    75                         cpus[i].stack = (uint8_t *) frame_alloc(STACK_FRAMES,
    76                             FRAME_LOWMEM | FRAME_KA | FRAME_ATOMIC);
     75                        uintptr_t stack_phys = frame_alloc(STACK_FRAMES,
     76                            FRAME_LOWMEM | FRAME_ATOMIC, STACK_SIZE - 1);
     77                        if (!stack_phys)
     78                                panic("Cannot allocate CPU stack.");
     79                       
     80                        cpus[i].stack = (uint8_t *) PA2KA(stack_phys);
    7781                        cpus[i].id = i;
    7882                       
    7983                        irq_spinlock_initialize(&cpus[i].lock, "cpus[].lock");
    8084                       
    81                         unsigned int j;
    82                         for (j = 0; j < RQ_COUNT; j++) {
     85                        for (unsigned int j = 0; j < RQ_COUNT; j++) {
    8386                                irq_spinlock_initialize(&cpus[i].rq[j].lock, "cpus[].rq[].lock");
    8487                                list_initialize(&cpus[i].rq[j].rq);
  • kernel/generic/src/ddi/ddi.c

    r3deb0155 rdd0c8a0  
    314314
    315315NO_TRACE static int dmamem_map(uintptr_t virt, size_t size, unsigned int map_flags,
    316     unsigned int flags, void **phys)
     316    unsigned int flags, uintptr_t *phys)
    317317{
    318318        ASSERT(TASK);
     
    322322}
    323323
    324 NO_TRACE static int dmamem_map_anonymous(size_t size, unsigned int map_flags,
    325     unsigned int flags, void **phys, uintptr_t *virt, uintptr_t bound)
     324NO_TRACE static int dmamem_map_anonymous(size_t size, uintptr_t constraint,
     325    unsigned int map_flags, unsigned int flags, uintptr_t *phys,
     326    uintptr_t *virt, uintptr_t bound)
    326327{
    327328        ASSERT(TASK);
    328329       
    329         size_t pages = SIZE2FRAMES(size);
    330         uint8_t order;
    331        
    332         /* We need the 2^order >= pages */
    333         if (pages == 1)
    334                 order = 0;
    335         else
    336                 order = fnzb(pages - 1) + 1;
    337        
    338         *phys = frame_alloc_noreserve(order, 0);
    339         if (*phys == NULL)
     330        size_t frames = SIZE2FRAMES(size);
     331        *phys = frame_alloc(frames, FRAME_NO_RESERVE, constraint);
     332        if (*phys == 0)
    340333                return ENOMEM;
    341334       
    342335        mem_backend_data_t backend_data;
    343         backend_data.base = (uintptr_t) *phys;
    344         backend_data.frames = pages;
     336        backend_data.base = *phys;
     337        backend_data.frames = frames;
    345338       
    346339        if (!as_area_create(TASK->as, map_flags, size,
    347340            AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
    348                 frame_free_noreserve((uintptr_t) *phys);
     341                frame_free_noreserve(*phys, frames);
    349342                return ENOMEM;
    350343        }
     
    373366                 */
    374367               
    375                 void *phys;
     368                uintptr_t phys;
    376369                int rc = dmamem_map((uintptr_t) virt_ptr, size, map_flags,
    377370                    flags, &phys);
     
    390383                 */
    391384               
    392                 void *phys;
     385                uintptr_t constraint;
     386                int rc = copy_from_uspace(&constraint, phys_ptr,
     387                    sizeof(constraint));
     388                if (rc != EOK)
     389                        return rc;
     390               
     391                uintptr_t phys;
    393392                uintptr_t virt = (uintptr_t) -1;
    394                 int rc = dmamem_map_anonymous(size, map_flags, flags,
     393                rc = dmamem_map_anonymous(size, constraint, map_flags, flags,
    395394                    &phys, &virt, bound);
    396395                if (rc != EOK)
  • kernel/generic/src/debug/panic.c

    r3deb0155 rdd0c8a0  
    9696        printf("THE=%p: ", THE);
    9797        if (THE != NULL) {
    98                 printf("pe=%" PRIun " thr=%p task=%p cpu=%p as=%p"
     98                printf("pd=%" PRIun " thread=%p task=%p cpu=%p as=%p"
    9999                    " magic=%#" PRIx32 "\n", THE->preemption_disabled,
    100100                    THE->thread, THE->task, THE->cpu, THE->as, THE->magic);
     101               
     102                if (THE->thread != NULL)
     103                        printf("thread=\"%s\"\n", THE->thread->name);
     104               
     105                if (THE->task != NULL)
     106                        printf("task=\"%s\"\n", THE->task->name);
    101107        } else
    102108                printf("invalid\n");
  • kernel/generic/src/debug/stacktrace.c

    r3deb0155 rdd0c8a0  
    3939#include <print.h>
    4040
    41 #define STACK_FRAMES_MAX        20
     41#define STACK_FRAMES_MAX  20
    4242
    4343void stack_trace_ctx(stack_trace_ops_t *ops, stack_trace_context_t *ctx)
     
    4949        uintptr_t pc;
    5050       
    51         while (cnt++ < STACK_FRAMES_MAX &&
    52             ops->stack_trace_context_validate(ctx)) {
     51        while ((cnt++ < STACK_FRAMES_MAX) &&
     52            (ops->stack_trace_context_validate(ctx))) {
    5353                if (ops->symbol_resolve &&
    5454                    ops->symbol_resolve(ctx->pc, &symbol, &offset)) {
  • kernel/generic/src/ipc/ipc.c

    r3deb0155 rdd0c8a0  
    774774static void ipc_print_call_list(list_t *list)
    775775{
    776         list_foreach(*list, cur) {
    777                 call_t *call = list_get_instance(cur, call_t, ab_link);
    778                
     776        list_foreach(*list, ab_link, call_t, call) {
    779777#ifdef __32_BITS__
    780778                printf("%10p ", call);
  • kernel/generic/src/ipc/ipcrsc.c

    r3deb0155 rdd0c8a0  
    151151        irq_spinlock_lock(&TASK->answerbox.lock, true);
    152152       
    153         list_foreach(TASK->answerbox.dispatched_calls, lst) {
    154                 call_t *call = list_get_instance(lst, call_t, ab_link);
     153        list_foreach(TASK->answerbox.dispatched_calls, ab_link, call_t, call) {
    155154                if ((sysarg_t) call == callid) {
    156155                        result = call;
  • kernel/generic/src/lib/ra.c

    r3deb0155 rdd0c8a0  
    391391
    392392        irq_spinlock_lock(&arena->lock, true);
    393         list_foreach(arena->spans, cur) {
    394                 ra_span_t *span = list_get_instance(cur, ra_span_t, span_link);
    395 
     393        list_foreach(arena->spans, span_link, ra_span_t, span) {
    396394                base = ra_span_alloc(span, size, alignment);
    397395                if (base)
     
    407405{
    408406        irq_spinlock_lock(&arena->lock, true);
    409         list_foreach(arena->spans, cur) {
    410                 ra_span_t *span = list_get_instance(cur, ra_span_t, span_link);
    411 
     407        list_foreach(arena->spans, span_link, ra_span_t, span) {
    412408                if (iswithin(span->base, span->size, base, size)) {
    413409                        ra_span_free(span, base, size);
  • kernel/generic/src/main/main.c

    r3deb0155 rdd0c8a0  
    8989#include <lib/ra.h>
    9090
     91/* Ensure [u]int*_t types are of correct size.
     92 *
     93 * Probably, this is not the best place for such tests
     94 * but this file is compiled on all architectures.
     95 */
     96#define CHECK_INT_TYPE_(signness, size) \
     97        STATIC_ASSERT_VERBOSE(sizeof(signness##size##_t) * 8 == size, \
     98            #signness #size "_t does not have " #size " bits");
     99
     100#define CHECK_INT_TYPE(size) \
     101        CHECK_INT_TYPE_(int, size); \
     102        CHECK_INT_TYPE_(uint, size)
     103
     104CHECK_INT_TYPE(8);
     105CHECK_INT_TYPE(16);
     106CHECK_INT_TYPE(32);
     107CHECK_INT_TYPE(64);
     108
    91109/** Global configuration structure. */
    92110config_t config = {
  • kernel/generic/src/mm/as.c

    r3deb0155 rdd0c8a0  
    488488       
    489489        /* Eventually check the addresses behind each area */
    490         list_foreach(as->as_area_btree.leaf_list, cur) {
    491                 btree_node_t *node =
    492                     list_get_instance(cur, btree_node_t, leaf_link);
     490        list_foreach(as->as_area_btree.leaf_list, leaf_link, btree_node_t, node) {
    493491               
    494492                for (btree_key_t i = 0; i < node->keys; i++) {
     
    904902                 * reference from all frames found there.
    905903                 */
    906                 list_foreach(sh_info->pagemap.leaf_list, cur) {
    907                         btree_node_t *node
    908                             = list_get_instance(cur, btree_node_t, leaf_link);
     904                list_foreach(sh_info->pagemap.leaf_list, leaf_link,
     905                    btree_node_t, node) {
    909906                        btree_key_t i;
    910907                       
    911908                        for (i = 0; i < node->keys; i++)
    912                                 frame_free((uintptr_t) node->value[i]);
     909                                frame_free((uintptr_t) node->value[i], 1);
    913910                }
    914911               
     
    956953         * Visit only the pages mapped by used_space B+tree.
    957954         */
    958         list_foreach(area->used_space.leaf_list, cur) {
    959                 btree_node_t *node;
     955        list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
     956            node) {
    960957                btree_key_t i;
    961958               
    962                 node = list_get_instance(cur, btree_node_t, leaf_link);
    963959                for (i = 0; i < node->keys; i++) {
    964960                        uintptr_t ptr = node->key[i];
     
    12381234        size_t used_pages = 0;
    12391235       
    1240         list_foreach(area->used_space.leaf_list, cur) {
    1241                 btree_node_t *node
    1242                     = list_get_instance(cur, btree_node_t, leaf_link);
     1236        list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
     1237            node) {
    12431238                btree_key_t i;
    12441239               
     
    12641259        size_t frame_idx = 0;
    12651260       
    1266         list_foreach(area->used_space.leaf_list, cur) {
    1267                 btree_node_t *node = list_get_instance(cur, btree_node_t,
    1268                     leaf_link);
     1261        list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
     1262            node) {
    12691263                btree_key_t i;
    12701264               
     
    13161310        frame_idx = 0;
    13171311       
    1318         list_foreach(area->used_space.leaf_list, cur) {
    1319                 btree_node_t *node
    1320                     = list_get_instance(cur, btree_node_t, leaf_link);
     1312        list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
     1313            node) {
    13211314                btree_key_t i;
    13221315               
     
    21822175        size_t area_cnt = 0;
    21832176       
    2184         list_foreach(as->as_area_btree.leaf_list, cur) {
    2185                 btree_node_t *node =
    2186                     list_get_instance(cur, btree_node_t, leaf_link);
     2177        list_foreach(as->as_area_btree.leaf_list, leaf_link, btree_node_t,
     2178            node) {
    21872179                area_cnt += node->keys;
    21882180        }
     
    21952187        size_t area_idx = 0;
    21962188       
    2197         list_foreach(as->as_area_btree.leaf_list, cur) {
    2198                 btree_node_t *node =
    2199                     list_get_instance(cur, btree_node_t, leaf_link);
     2189        list_foreach(as->as_area_btree.leaf_list, leaf_link, btree_node_t,
     2190            node) {
    22002191                btree_key_t i;
    22012192               
     
    22312222       
    22322223        /* Print out info about address space areas */
    2233         list_foreach(as->as_area_btree.leaf_list, cur) {
    2234                 btree_node_t *node
    2235                     = list_get_instance(cur, btree_node_t, leaf_link);
     2224        list_foreach(as->as_area_btree.leaf_list, leaf_link, btree_node_t,
     2225            node) {
    22362226                btree_key_t i;
    22372227               
  • kernel/generic/src/mm/backend_anon.c

    r3deb0155 rdd0c8a0  
    118118         */
    119119        mutex_lock(&area->sh_info->lock);
    120         list_foreach(area->used_space.leaf_list, cur) {
    121                 btree_node_t *node;
     120        list_foreach(area->used_space.leaf_list, leaf_link, btree_node_t,
     121            node) {
    122122                unsigned int i;
    123123               
    124                 node = list_get_instance(cur, btree_node_t, leaf_link);
    125124                for (i = 0; i < node->keys; i++) {
    126125                        uintptr_t base = node->key[i];
     
    295294                 * the normal unreserving frame_free().
    296295                 */
    297                 frame_free(frame);
     296                frame_free(frame, 1);
    298297        } else {
    299298                /*
     
    302301                 * manipulate the reserve or it would be given back twice.
    303302                 */
    304                 frame_free_noreserve(frame);
     303                frame_free_noreserve(frame, 1);
    305304        }
    306305}
  • kernel/generic/src/mm/backend_elf.c

    r3deb0155 rdd0c8a0  
    429429                         * data.
    430430                         */
    431                         frame_free_noreserve(frame);
     431                        frame_free_noreserve(frame, 1);
    432432                }
    433433        } else {
     
    437437                 * anonymous). In any case, a frame needs to be freed.
    438438                 */
    439                 frame_free_noreserve(frame);
     439                frame_free_noreserve(frame, 1);
    440440        }
    441441}
  • kernel/generic/src/mm/frame.c

    r3deb0155 rdd0c8a0  
    3838 *
    3939 * This file contains the physical frame allocator and memory zone management.
    40  * The frame allocator is built on top of the buddy allocator.
    41  *
    42  * @see buddy.c
     40 * The frame allocator is built on top of the two-level bitmap structure.
     41 *
    4342 */
    4443
     
    9291}
    9392
    94 NO_TRACE static inline size_t make_frame_index(zone_t *zone, frame_t *frame)
    95 {
    96         return (frame - zone->frames);
    97 }
    98 
    9993/** Initialize frame structure.
    10094 *
     
    10498NO_TRACE static void frame_initialize(frame_t *frame)
    10599{
    106         frame->refcount = 1;
    107         frame->buddy_order = 0;
     100        frame->refcount = 0;
     101        frame->parent = NULL;
    108102}
    109103
     
    161155       
    162156        /* Move other zones up */
    163         size_t j;
    164         for (j = zones.count; j > i; j--) {
     157        for (size_t j = zones.count; j > i; j--)
    165158                zones.info[j] = zones.info[j - 1];
    166                 if (zones.info[j].buddy_system != NULL)
    167                         zones.info[j].buddy_system->data =
    168                             (void *) &zones.info[j];
    169         }
    170159       
    171160        zones.count++;
     
    237226}
    238227
    239 /** @return True if zone can allocate specified order */
    240 NO_TRACE static bool zone_can_alloc(zone_t *zone, uint8_t order)
    241 {
     228/** @return True if zone can allocate specified number of frames */
     229NO_TRACE static bool zone_can_alloc(zone_t *zone, size_t count,
     230    pfn_t constraint)
     231{
     232        /*
     233         * The function bitmap_allocate_range() does not modify
     234         * the bitmap if the last argument is NULL.
     235         */
     236       
    242237        return ((zone->flags & ZONE_AVAILABLE) &&
    243             buddy_system_can_alloc(zone->buddy_system, order));
    244 }
    245 
    246 /** Find a zone that can allocate order frames.
     238            bitmap_allocate_range(&zone->bitmap, count, zone->base,
     239            FRAME_LOWPRIO, constraint, NULL));
     240}
     241
     242/** Find a zone that can allocate specified number of frames
     243 *
     244 * This function searches among all zones. Assume interrupts are
     245 * disabled and zones lock is locked.
     246 *
     247 * @param count      Number of free frames we are trying to find.
     248 * @param flags      Required flags of the zone.
     249 * @param constraint Indication of bits that cannot be set in the
     250 *                   physical frame number of the first allocated frame.
     251 * @param hint       Preferred zone.
     252 *
     253 * @return Zone that can allocate specified number of frames.
     254 * @return -1 if no zone can satisfy the request.
     255 *
     256 */
     257NO_TRACE static size_t find_free_zone_all(size_t count, zone_flags_t flags,
     258    pfn_t constraint, size_t hint)
     259{
     260        for (size_t pos = 0; pos < zones.count; pos++) {
     261                size_t i = (pos + hint) % zones.count;
     262               
     263                /* Check whether the zone meets the search criteria. */
     264                if (!ZONE_FLAGS_MATCH(zones.info[i].flags, flags))
     265                        continue;
     266               
     267                /* Check if the zone can satisfy the allocation request. */
     268                if (zone_can_alloc(&zones.info[i], count, constraint))
     269                        return i;
     270        }
     271       
     272        return (size_t) -1;
     273}
     274
     275/** Check if frame range  priority memory
     276 *
     277 * @param pfn   Starting frame.
     278 * @param count Number of frames.
     279 *
     280 * @return True if the range contains only priority memory.
     281 *
     282 */
     283NO_TRACE static bool is_high_priority(pfn_t base, size_t count)
     284{
     285        return (base + count <= FRAME_LOWPRIO);
     286}
     287
     288/** Find a zone that can allocate specified number of frames
     289 *
     290 * This function ignores zones that contain only high-priority
     291 * memory. Assume interrupts are disabled and zones lock is locked.
     292 *
     293 * @param count      Number of free frames we are trying to find.
     294 * @param flags      Required flags of the zone.
     295 * @param constraint Indication of bits that cannot be set in the
     296 *                   physical frame number of the first allocated frame.
     297 * @param hint       Preferred zone.
     298 *
     299 * @return Zone that can allocate specified number of frames.
     300 * @return -1 if no low-priority zone can satisfy the request.
     301 *
     302 */
     303NO_TRACE static size_t find_free_zone_lowprio(size_t count, zone_flags_t flags,
     304    pfn_t constraint, size_t hint)
     305{       
     306        for (size_t pos = 0; pos < zones.count; pos++) {
     307                size_t i = (pos + hint) % zones.count;
     308               
     309                /* Skip zones containing only high-priority memory. */
     310                if (is_high_priority(zones.info[i].base, zones.info[i].count))
     311                        continue;
     312               
     313                /* Check whether the zone meets the search criteria. */
     314                if (!ZONE_FLAGS_MATCH(zones.info[i].flags, flags))
     315                        continue;
     316               
     317                /* Check if the zone can satisfy the allocation request. */
     318                if (zone_can_alloc(&zones.info[i], count, constraint))
     319                        return i;
     320        }
     321       
     322        return (size_t) -1;
     323}
     324
     325/** Find a zone that can allocate specified number of frames
    247326 *
    248327 * Assume interrupts are disabled and zones lock is
    249328 * locked.
    250329 *
    251  * @param order Size (2^order) of free space we are trying to find.
    252  * @param flags Required flags of the target zone.
    253  * @param hind  Preferred zone.
    254  *
    255  */
    256 NO_TRACE static size_t find_free_zone(uint8_t order, zone_flags_t flags,
    257     size_t hint)
     330 * @param count      Number of free frames we are trying to find.
     331 * @param flags      Required flags of the target zone.
     332 * @param constraint Indication of bits that cannot be set in the
     333 *                   physical frame number of the first allocated frame.
     334 * @param hint       Preferred zone.
     335 *
     336 * @return Zone that can allocate specified number of frames.
     337 * @return -1 if no zone can satisfy the request.
     338 *
     339 */
     340NO_TRACE static size_t find_free_zone(size_t count, zone_flags_t flags,
     341    pfn_t constraint, size_t hint)
    258342{
    259343        if (hint >= zones.count)
    260344                hint = 0;
    261345       
    262         size_t i = hint;
    263         do {
    264                 /*
    265                  * Check whether the zone meets the search criteria.
    266                  */
    267                 if (ZONE_FLAGS_MATCH(zones.info[i].flags, flags)) {
    268                         /*
    269                          * Check if the zone has 2^order frames area available.
    270                          */
    271                         if (zone_can_alloc(&zones.info[i], order))
    272                                 return i;
    273                 }
    274                
    275                 i++;
    276                 if (i >= zones.count)
    277                         i = 0;
    278                
    279         } while (i != hint);
    280        
    281         return (size_t) -1;
    282 }
    283 
    284 /**************************/
    285 /* Buddy system functions */
    286 /**************************/
    287 
    288 /** Buddy system find_block implementation.
    289  *
    290  * Find block that is parent of current list.
    291  * That means go to lower addresses, until such block is found
    292  *
    293  * @param order Order of parent must be different then this
    294  *              parameter!!
    295  *
    296  */
    297 NO_TRACE static link_t *zone_buddy_find_block(buddy_system_t *buddy,
    298     link_t *child, uint8_t order)
    299 {
    300         frame_t *frame = list_get_instance(child, frame_t, buddy_link);
    301         zone_t *zone = (zone_t *) buddy->data;
    302        
    303         size_t index = frame_index(zone, frame);
    304         do {
    305                 if (zone->frames[index].buddy_order != order)
    306                         return &zone->frames[index].buddy_link;
    307         } while (index-- > 0);
    308        
    309         return NULL;
    310 }
    311 
    312 /** Buddy system find_buddy implementation.
    313  *
    314  * @param buddy Buddy system.
    315  * @param block Block for which buddy should be found.
    316  *
    317  * @return Buddy for given block if found.
    318  *
    319  */
    320 NO_TRACE static link_t *zone_buddy_find_buddy(buddy_system_t *buddy,
    321     link_t *block)
    322 {
    323         frame_t *frame = list_get_instance(block, frame_t, buddy_link);
    324         zone_t *zone = (zone_t *) buddy->data;
    325         ASSERT(IS_BUDDY_ORDER_OK(frame_index_abs(zone, frame),
    326             frame->buddy_order));
    327        
    328         bool is_left = IS_BUDDY_LEFT_BLOCK_ABS(zone, frame);
    329        
    330         size_t index;
    331         if (is_left) {
    332                 index = (frame_index(zone, frame)) +
    333                     (1 << frame->buddy_order);
    334         } else {  /* is_right */
    335                 index = (frame_index(zone, frame)) -
    336                     (1 << frame->buddy_order);
    337         }
    338        
    339         if (frame_index_valid(zone, index)) {
    340                 if ((zone->frames[index].buddy_order == frame->buddy_order) &&
    341                     (zone->frames[index].refcount == 0)) {
    342                         return &zone->frames[index].buddy_link;
    343                 }
    344         }
    345        
    346         return NULL;
    347 }
    348 
    349 /** Buddy system bisect implementation.
    350  *
    351  * @param buddy Buddy system.
    352  * @param block Block to bisect.
    353  *
    354  * @return Right block.
    355  *
    356  */
    357 NO_TRACE static link_t *zone_buddy_bisect(buddy_system_t *buddy, link_t *block)
    358 {
    359         frame_t *frame_l = list_get_instance(block, frame_t, buddy_link);
    360         frame_t *frame_r = (frame_l + (1 << (frame_l->buddy_order - 1)));
    361        
    362         return &frame_r->buddy_link;
    363 }
    364 
    365 /** Buddy system coalesce implementation.
    366  *
    367  * @param buddy   Buddy system.
    368  * @param block_1 First block.
    369  * @param block_2 First block's buddy.
    370  *
    371  * @return Coalesced block (actually block that represents lower
    372  *         address).
    373  *
    374  */
    375 NO_TRACE static link_t *zone_buddy_coalesce(buddy_system_t *buddy,
    376     link_t *block_1, link_t *block_2)
    377 {
    378         frame_t *frame1 = list_get_instance(block_1, frame_t, buddy_link);
    379         frame_t *frame2 = list_get_instance(block_2, frame_t, buddy_link);
    380        
    381         return ((frame1 < frame2) ? block_1 : block_2);
    382 }
    383 
    384 /** Buddy system set_order implementation.
    385  *
    386  * @param buddy Buddy system.
    387  * @param block Buddy system block.
    388  * @param order Order to set.
    389  *
    390  */
    391 NO_TRACE static void zone_buddy_set_order(buddy_system_t *buddy, link_t *block,
    392     uint8_t order)
    393 {
    394         list_get_instance(block, frame_t, buddy_link)->buddy_order = order;
    395 }
    396 
    397 /** Buddy system get_order implementation.
    398  *
    399  * @param buddy Buddy system.
    400  * @param block Buddy system block.
    401  *
    402  * @return Order of block.
    403  *
    404  */
    405 NO_TRACE static uint8_t zone_buddy_get_order(buddy_system_t *buddy,
    406     link_t *block)
    407 {
    408         return list_get_instance(block, frame_t, buddy_link)->buddy_order;
    409 }
    410 
    411 /** Buddy system mark_busy implementation.
    412  *
    413  * @param buddy Buddy system.
    414  * @param block Buddy system block.
    415  *
    416  */
    417 NO_TRACE static void zone_buddy_mark_busy(buddy_system_t *buddy, link_t *block)
    418 {
    419         list_get_instance(block, frame_t, buddy_link)->refcount = 1;
    420 }
    421 
    422 /** Buddy system mark_available implementation.
    423  *
    424  * @param buddy Buddy system.
    425  * @param block Buddy system block.
    426  *
    427  */
    428 NO_TRACE static void zone_buddy_mark_available(buddy_system_t *buddy,
    429     link_t *block)
    430 {
    431         list_get_instance(block, frame_t, buddy_link)->refcount = 0;
    432 }
    433 
    434 static buddy_system_operations_t zone_buddy_system_operations = {
    435         .find_buddy = zone_buddy_find_buddy,
    436         .bisect = zone_buddy_bisect,
    437         .coalesce = zone_buddy_coalesce,
    438         .set_order = zone_buddy_set_order,
    439         .get_order = zone_buddy_get_order,
    440         .mark_busy = zone_buddy_mark_busy,
    441         .mark_available = zone_buddy_mark_available,
    442         .find_block = zone_buddy_find_block
    443 };
     346        /*
     347         * Prefer zones with low-priority memory over
     348         * zones with high-priority memory.
     349         */
     350       
     351        size_t znum = find_free_zone_lowprio(count, flags, constraint, hint);
     352        if (znum != (size_t) -1)
     353                return znum;
     354       
     355        /* Take all zones into account */
     356        return find_free_zone_all(count, flags, constraint, hint);
     357}
    444358
    445359/******************/
     
    447361/******************/
    448362
     363/** Return frame from zone. */
     364NO_TRACE static frame_t *zone_get_frame(zone_t *zone, size_t index)
     365{
     366        ASSERT(index < zone->count);
     367       
     368        return &zone->frames[index];
     369}
     370
    449371/** Allocate frame in particular zone.
    450372 *
     
    452374 * Panics if allocation is impossible.
    453375 *
    454  * @param zone  Zone to allocate from.
    455  * @param order Allocate exactly 2^order frames.
     376 * @param zone       Zone to allocate from.
     377 * @param count      Number of frames to allocate
     378 * @param constraint Indication of bits that cannot be set in the
     379 *                   physical frame number of the first allocated frame.
    456380 *
    457381 * @return Frame index in zone.
    458382 *
    459383 */
    460 NO_TRACE static pfn_t zone_frame_alloc(zone_t *zone, uint8_t order)
     384NO_TRACE static size_t zone_frame_alloc(zone_t *zone, size_t count,
     385    pfn_t constraint)
    461386{
    462387        ASSERT(zone->flags & ZONE_AVAILABLE);
    463388       
    464         /* Allocate frames from zone buddy system */
    465         link_t *link = buddy_system_alloc(zone->buddy_system, order);
    466        
    467         ASSERT(link);
     389        /* Allocate frames from zone */
     390        size_t index;
     391        int avail = bitmap_allocate_range(&zone->bitmap, count, zone->base,
     392            FRAME_LOWPRIO, constraint, &index);
     393       
     394        ASSERT(avail);
     395       
     396        /* Update frame reference count */
     397        for (size_t i = 0; i < count; i++) {
     398                frame_t *frame = zone_get_frame(zone, index + i);
     399               
     400                ASSERT(frame->refcount == 0);
     401                frame->refcount = 1;
     402        }
    468403       
    469404        /* Update zone information. */
    470         zone->free_count -= (1 << order);
    471         zone->busy_count += (1 << order);
    472        
    473         /* Frame will be actually a first frame of the block. */
    474         frame_t *frame = list_get_instance(link, frame_t, buddy_link);
    475        
    476         /* Get frame address */
    477         return make_frame_index(zone, frame);
     405        zone->free_count -= count;
     406        zone->busy_count += count;
     407       
     408        return index;
    478409}
    479410
     
    482413 * Assume zone is locked and is available for deallocation.
    483414 *
    484  * @param zone      Pointer to zone from which the frame is to be freed.
    485  * @param frame_idx Frame index relative to zone.
    486  *
    487  * @return          Number of freed frames.
    488  *
    489  */
    490 NO_TRACE static size_t zone_frame_free(zone_t *zone, size_t frame_idx)
     415 * @param zone  Pointer to zone from which the frame is to be freed.
     416 * @param index Frame index relative to zone.
     417 *
     418 * @return Number of freed frames.
     419 *
     420 */
     421NO_TRACE static size_t zone_frame_free(zone_t *zone, size_t index)
    491422{
    492423        ASSERT(zone->flags & ZONE_AVAILABLE);
    493424       
    494         frame_t *frame = &zone->frames[frame_idx];
    495         size_t size = 0;
    496        
    497         ASSERT(frame->refcount);
     425        frame_t *frame = zone_get_frame(zone, index);
     426       
     427        ASSERT(frame->refcount > 0);
    498428       
    499429        if (!--frame->refcount) {
    500                 size = 1 << frame->buddy_order;
    501                 buddy_system_free(zone->buddy_system, &frame->buddy_link);             
     430                bitmap_set(&zone->bitmap, index, 0);
     431               
    502432                /* Update zone information. */
    503                 zone->free_count += size;
    504                 zone->busy_count -= size;
    505         }
    506        
    507         return size;
    508 }
    509 
    510 /** Return frame from zone. */
    511 NO_TRACE static frame_t *zone_get_frame(zone_t *zone, size_t frame_idx)
    512 {
    513         ASSERT(frame_idx < zone->count);
    514         return &zone->frames[frame_idx];
     433                zone->free_count++;
     434                zone->busy_count--;
     435               
     436                return 1;
     437        }
     438       
     439        return 0;
    515440}
    516441
    517442/** Mark frame in zone unavailable to allocation. */
    518 NO_TRACE static void zone_mark_unavailable(zone_t *zone, size_t frame_idx)
     443NO_TRACE static void zone_mark_unavailable(zone_t *zone, size_t index)
    519444{
    520445        ASSERT(zone->flags & ZONE_AVAILABLE);
    521446       
    522         frame_t *frame = zone_get_frame(zone, frame_idx);
    523         if (frame->refcount)
     447        frame_t *frame = zone_get_frame(zone, index);
     448        if (frame->refcount > 0)
    524449                return;
    525450       
    526         link_t *link __attribute__ ((unused));
    527        
    528         link = buddy_system_alloc_block(zone->buddy_system,
    529             &frame->buddy_link);
    530        
    531         ASSERT(link);
     451        frame->refcount = 1;
     452        bitmap_set_range(&zone->bitmap, index, 1);
     453       
    532454        zone->free_count--;
    533455        reserve_force_alloc(1);
     
    536458/** Merge two zones.
    537459 *
    538  * Expect buddy to point to space at least zone_conf_size large.
    539460 * Assume z1 & z2 are locked and compatible and zones lock is
    540461 * locked.
    541462 *
    542  * @param z1     First zone to merge.
    543  * @param z2     Second zone to merge.
    544  * @param old_z1 Original date of the first zone.
    545  * @param buddy  Merged zone buddy.
     463 * @param z1       First zone to merge.
     464 * @param z2       Second zone to merge.
     465 * @param old_z1   Original data of the first zone.
     466 * @param confdata Merged zone configuration data.
    546467 *
    547468 */
    548469NO_TRACE static void zone_merge_internal(size_t z1, size_t z2, zone_t *old_z1,
    549     buddy_system_t *buddy)
     470    void *confdata)
    550471{
    551472        ASSERT(zones.info[z1].flags & ZONE_AVAILABLE);
     
    562483        zones.info[z1].free_count += zones.info[z2].free_count;
    563484        zones.info[z1].busy_count += zones.info[z2].busy_count;
    564         zones.info[z1].buddy_system = buddy;
    565        
    566         uint8_t order = fnzb(zones.info[z1].count);
    567         buddy_system_create(zones.info[z1].buddy_system, order,
    568             &zone_buddy_system_operations, (void *) &zones.info[z1]);
    569        
    570         zones.info[z1].frames =
    571             (frame_t *) ((uint8_t *) zones.info[z1].buddy_system
    572             + buddy_conf_size(order));
    573        
    574         /* This marks all frames busy */
    575         size_t i;
    576         for (i = 0; i < zones.info[z1].count; i++)
    577                 frame_initialize(&zones.info[z1].frames[i]);
    578        
    579         /* Copy frames from both zones to preserve full frame orders,
    580          * parents etc. Set all free frames with refcount = 0 to 1, because
    581          * we add all free frames to buddy allocator later again, clearing
    582          * order to 0. Don't set busy frames with refcount = 0, as they
    583          * will not be reallocated during merge and it would make later
    584          * problems with allocation/free.
     485       
     486        bitmap_initialize(&zones.info[z1].bitmap, zones.info[z1].count,
     487            confdata + (sizeof(frame_t) * zones.info[z1].count));
     488        bitmap_clear_range(&zones.info[z1].bitmap, 0, zones.info[z1].count);
     489       
     490        zones.info[z1].frames = (frame_t *) confdata;
     491       
     492        /*
     493         * Copy frames and bits from both zones to preserve parents, etc.
    585494         */
    586         for (i = 0; i < old_z1->count; i++)
     495       
     496        for (size_t i = 0; i < old_z1->count; i++) {
     497                bitmap_set(&zones.info[z1].bitmap, i,
     498                    bitmap_get(&old_z1->bitmap, i));
    587499                zones.info[z1].frames[i] = old_z1->frames[i];
    588        
    589         for (i = 0; i < zones.info[z2].count; i++)
    590                 zones.info[z1].frames[base_diff + i]
    591                     = zones.info[z2].frames[i];
    592        
    593         i = 0;
    594         while (i < zones.info[z1].count) {
    595                 if (zones.info[z1].frames[i].refcount) {
    596                         /* Skip busy frames */
    597                         i += 1 << zones.info[z1].frames[i].buddy_order;
    598                 } else {
    599                         /* Free frames, set refcount = 1
    600                          * (all free frames have refcount == 0, we need not
    601                          * to check the order)
    602                          */
    603                         zones.info[z1].frames[i].refcount = 1;
    604                         zones.info[z1].frames[i].buddy_order = 0;
    605                         i++;
    606                 }
    607         }
    608        
    609         /* Add free blocks from the original zone z1 */
    610         while (zone_can_alloc(old_z1, 0)) {
    611                 /* Allocate from the original zone */
    612                 pfn_t frame_idx = zone_frame_alloc(old_z1, 0);
    613                
    614                 /* Free the frame from the merged zone */
    615                 frame_t *frame = &zones.info[z1].frames[frame_idx];
    616                 frame->refcount = 0;
    617                 buddy_system_free(zones.info[z1].buddy_system, &frame->buddy_link);
    618         }
    619        
    620         /* Add free blocks from the original zone z2 */
    621         while (zone_can_alloc(&zones.info[z2], 0)) {
    622                 /* Allocate from the original zone */
    623                 pfn_t frame_idx = zone_frame_alloc(&zones.info[z2], 0);
    624                
    625                 /* Free the frame from the merged zone */
    626                 frame_t *frame = &zones.info[z1].frames[base_diff + frame_idx];
    627                 frame->refcount = 0;
    628                 buddy_system_free(zones.info[z1].buddy_system, &frame->buddy_link);
     500        }
     501       
     502        for (size_t i = 0; i < zones.info[z2].count; i++) {
     503                bitmap_set(&zones.info[z1].bitmap, base_diff + i,
     504                    bitmap_get(&zones.info[z2].bitmap, i));
     505                zones.info[z1].frames[base_diff + i] =
     506                    zones.info[z2].frames[i];
    629507        }
    630508}
     
    649527        size_t cframes = SIZE2FRAMES(zone_conf_size(count));
    650528       
    651         if ((pfn < zones.info[znum].base)
    652             || (pfn >= zones.info[znum].base + zones.info[znum].count))
     529        if ((pfn < zones.info[znum].base) ||
     530            (pfn >= zones.info[znum].base + zones.info[znum].count))
    653531                return;
    654532       
    655         frame_t *frame __attribute__ ((unused));
    656 
    657         frame = &zones.info[znum].frames[pfn - zones.info[znum].base];
    658         ASSERT(!frame->buddy_order);
    659        
    660         size_t i;
    661         for (i = 0; i < cframes; i++) {
    662                 zones.info[znum].busy_count++;
     533        for (size_t i = 0; i < cframes; i++)
    663534                (void) zone_frame_free(&zones.info[znum],
    664535                    pfn - zones.info[znum].base + i);
    665         }
    666 }
    667 
    668 /** Reduce allocated block to count of order 0 frames.
    669  *
    670  * The allocated block needs 2^order frames. Reduce all frames
    671  * in the block to order 0 and free the unneeded frames. This means that
    672  * when freeing the previously allocated block starting with frame_idx,
    673  * you have to free every frame.
    674  *
    675  * @param znum      Zone.
    676  * @param frame_idx Index the first frame of the block.
    677  * @param count     Allocated frames in block.
    678  *
    679  */
    680 NO_TRACE static void zone_reduce_region(size_t znum, pfn_t frame_idx,
    681     size_t count)
    682 {
    683         ASSERT(zones.info[znum].flags & ZONE_AVAILABLE);
    684         ASSERT(frame_idx + count < zones.info[znum].count);
    685        
    686         uint8_t order = zones.info[znum].frames[frame_idx].buddy_order;
    687         ASSERT((size_t) (1 << order) >= count);
    688        
    689         /* Reduce all blocks to order 0 */
    690         size_t i;
    691         for (i = 0; i < (size_t) (1 << order); i++) {
    692                 frame_t *frame = &zones.info[znum].frames[i + frame_idx];
    693                 frame->buddy_order = 0;
    694                 if (!frame->refcount)
    695                         frame->refcount = 1;
    696                 ASSERT(frame->refcount == 1);
    697         }
    698        
    699         /* Free unneeded frames */
    700         for (i = count; i < (size_t) (1 << order); i++)
    701                 (void) zone_frame_free(&zones.info[znum], i + frame_idx);
    702536}
    703537
     
    719553        bool ret = true;
    720554       
    721         /* We can join only 2 zones with none existing inbetween,
     555        /*
     556         * We can join only 2 zones with none existing inbetween,
    722557         * the zones have to be available and with the same
    723558         * set of flags
     
    733568            + zones.info[z2].count));
    734569       
    735         uint8_t order;
    736         if (cframes == 1)
    737                 order = 0;
    738         else
    739                 order = fnzb(cframes - 1) + 1;
    740        
    741570        /* Allocate merged zone data inside one of the zones */
    742571        pfn_t pfn;
    743         if (zone_can_alloc(&zones.info[z1], order)) {
    744                 pfn = zones.info[z1].base + zone_frame_alloc(&zones.info[z1], order);
    745         } else if (zone_can_alloc(&zones.info[z2], order)) {
    746                 pfn = zones.info[z2].base + zone_frame_alloc(&zones.info[z2], order);
     572        if (zone_can_alloc(&zones.info[z1], cframes, 0)) {
     573                pfn = zones.info[z1].base +
     574                    zone_frame_alloc(&zones.info[z1], cframes, 0);
     575        } else if (zone_can_alloc(&zones.info[z2], cframes, 0)) {
     576                pfn = zones.info[z2].base +
     577                    zone_frame_alloc(&zones.info[z2], cframes, 0);
    747578        } else {
    748579                ret = false;
     
    752583        /* Preserve original data from z1 */
    753584        zone_t old_z1 = zones.info[z1];
    754         old_z1.buddy_system->data = (void *) &old_z1;
    755585       
    756586        /* Do zone merging */
    757         buddy_system_t *buddy = (buddy_system_t *) PA2KA(PFN2ADDR(pfn));
    758         zone_merge_internal(z1, z2, &old_z1, buddy);
    759        
    760         /* Free unneeded config frames */
    761         zone_reduce_region(z1, pfn - zones.info[z1].base, cframes);
     587        zone_merge_internal(z1, z2, &old_z1, (void *) PA2KA(PFN2ADDR(pfn)));
    762588       
    763589        /* Subtract zone information from busy frames */
     
    772598       
    773599        /* Move zones down */
    774         size_t i;
    775         for (i = z2 + 1; i < zones.count; i++) {
     600        for (size_t i = z2 + 1; i < zones.count; i++)
    776601                zones.info[i - 1] = zones.info[i];
    777                 if (zones.info[i - 1].buddy_system != NULL)
    778                         zones.info[i - 1].buddy_system->data =
    779                             (void *) &zones.info[i - 1];
    780         }
    781602       
    782603        zones.count--;
     
    797618void zone_merge_all(void)
    798619{
    799         size_t i = 0;
     620        size_t i = 1;
     621       
    800622        while (i < zones.count) {
    801                 if (!zone_merge(i, i + 1))
     623                if (!zone_merge(i - 1, i))
    802624                        i++;
    803625        }
     
    806628/** Create new frame zone.
    807629 *
    808  * @param zone  Zone to construct.
    809  * @param buddy Address of buddy system configuration information.
    810  * @param start Physical address of the first frame within the zone.
    811  * @param count Count of frames in zone.
    812  * @param flags Zone flags.
     630 * @param zone     Zone to construct.
     631 * @param start    Physical address of the first frame within the zone.
     632 * @param count    Count of frames in zone.
     633 * @param flags    Zone flags.
     634 * @param confdata Configuration data of the zone.
    813635 *
    814636 * @return Initialized zone.
    815637 *
    816638 */
    817 NO_TRACE static void zone_construct(zone_t *zone, buddy_system_t *buddy,
    818     pfn_t start, size_t count, zone_flags_t flags)
     639NO_TRACE static void zone_construct(zone_t *zone, pfn_t start, size_t count,
     640    zone_flags_t flags, void *confdata)
    819641{
    820642        zone->base = start;
     
    823645        zone->free_count = count;
    824646        zone->busy_count = 0;
    825         zone->buddy_system = buddy;
    826647       
    827648        if (flags & ZONE_AVAILABLE) {
    828649                /*
    829                  * Compute order for buddy system and initialize
     650                 * Initialize frame bitmap (located after the array of
     651                 * frame_t structures in the configuration space).
    830652                 */
    831                 uint8_t order = fnzb(count);
    832                 buddy_system_create(zone->buddy_system, order,
    833                     &zone_buddy_system_operations, (void *) zone);
    834                
    835                 /* Allocate frames _after_ the confframe */
    836                
    837                 /* Check sizes */
    838                 zone->frames = (frame_t *) ((uint8_t *) zone->buddy_system +
    839                     buddy_conf_size(order));
    840                
    841                 size_t i;
    842                 for (i = 0; i < count; i++)
     653               
     654                bitmap_initialize(&zone->bitmap, count, confdata +
     655                    (sizeof(frame_t) * count));
     656                bitmap_clear_range(&zone->bitmap, 0, count);
     657               
     658                /*
     659                 * Initialize the array of frame_t structures.
     660                 */
     661               
     662                zone->frames = (frame_t *) confdata;
     663               
     664                for (size_t i = 0; i < count; i++)
    843665                        frame_initialize(&zone->frames[i]);
    844                
    845                 /* Stuffing frames */
    846                 for (i = 0; i < count; i++) {
    847                         zone->frames[i].refcount = 0;
    848                         buddy_system_free(zone->buddy_system, &zone->frames[i].buddy_link);
    849                 }
    850         } else
     666        } else {
     667                bitmap_initialize(&zone->bitmap, 0, NULL);
    851668                zone->frames = NULL;
     669        }
    852670}
    853671
     
    861679size_t zone_conf_size(size_t count)
    862680{
    863         return (count * sizeof(frame_t) + buddy_conf_size(fnzb(count)));
     681        return (count * sizeof(frame_t) + bitmap_size(count));
    864682}
    865683
     
    867685pfn_t zone_external_conf_alloc(size_t count)
    868686{
    869         size_t size = zone_conf_size(count);
    870         size_t order = ispwr2(size) ? fnzb(size) : (fnzb(size) + 1);
    871 
    872         return ADDR2PFN((uintptr_t) frame_alloc(order - FRAME_WIDTH,
    873             FRAME_LOWMEM | FRAME_ATOMIC));
     687        size_t frames = SIZE2FRAMES(zone_conf_size(count));
     688       
     689        return ADDR2PFN((uintptr_t)
     690            frame_alloc(frames, FRAME_LOWMEM | FRAME_ATOMIC, 0));
    874691}
    875692
     
    879696 * @param count     Size of zone in frames.
    880697 * @param confframe Where configuration frames are supposed to be.
    881  *                  Automatically checks, that we will not disturb the
     698 *                  Automatically checks that we will not disturb the
    882699 *                  kernel and possibly init. If confframe is given
    883700 *                  _outside_ this zone, it is expected, that the area is
     
    896713       
    897714        if (flags & ZONE_AVAILABLE) {  /* Create available zone */
    898                 /* Theoretically we could have NULL here, practically make sure
     715                /*
     716                 * Theoretically we could have NULL here, practically make sure
    899717                 * nobody tries to do that. If some platform requires, remove
    900718                 * the assert
    901719                 */
    902720                ASSERT(confframe != ADDR2PFN((uintptr_t ) NULL));
    903 
     721               
    904722                /* Update the known end of physical memory. */
    905723                config.physmem_end = max(config.physmem_end, PFN2ADDR(start + count));
    906724               
    907                 /* If confframe is supposed to be inside our zone, then make sure
     725                /*
     726                 * If confframe is supposed to be inside our zone, then make sure
    908727                 * it does not span kernel & init
    909728                 */
    910729                size_t confcount = SIZE2FRAMES(zone_conf_size(count));
     730               
    911731                if ((confframe >= start) && (confframe < start + count)) {
    912732                        for (; confframe < start + count; confframe++) {
     
    921741                               
    922742                                bool overlap = false;
    923                                 size_t i;
    924                                 for (i = 0; i < init.cnt; i++)
     743                                for (size_t i = 0; i < init.cnt; i++) {
    925744                                        if (overlaps(addr, PFN2ADDR(confcount),
    926745                                            init.tasks[i].paddr,
     
    929748                                                break;
    930749                                        }
     750                                }
     751                               
    931752                                if (overlap)
    932753                                        continue;
     
    945766                }
    946767               
    947                 buddy_system_t *buddy = (buddy_system_t *) PA2KA(PFN2ADDR(confframe));
    948                 zone_construct(&zones.info[znum], buddy, start, count, flags);
     768                void *confdata = (void *) PA2KA(PFN2ADDR(confframe));
     769                zone_construct(&zones.info[znum], start, count, flags, confdata);
    949770               
    950771                /* If confdata in zone, mark as unavailable */
    951772                if ((confframe >= start) && (confframe < start + count)) {
    952                         size_t i;
    953                         for (i = confframe; i < confframe + confcount; i++)
     773                        for (size_t i = confframe; i < confframe + confcount; i++)
    954774                                zone_mark_unavailable(&zones.info[znum],
    955775                                    i - zones.info[znum].base);
     
    967787                return (size_t) -1;
    968788        }
    969         zone_construct(&zones.info[znum], NULL, start, count, flags);
     789       
     790        zone_construct(&zones.info[znum], start, count, flags, NULL);
    970791       
    971792        irq_spinlock_unlock(&zones.lock, true);
     
    1009830}
    1010831
    1011 /** Allocate power-of-two frames of physical memory.
    1012  *
    1013  * @param order Allocate exactly 2^order frames.
    1014  * @param flags Flags for host zone selection and address processing.
    1015  * @param pzone Preferred zone.
     832/** Allocate frames of physical memory.
     833 *
     834 * @param count      Number of continuous frames to allocate.
     835 * @param flags      Flags for host zone selection and address processing.
     836 * @param constraint Indication of physical address bits that cannot be
     837 *                   set in the address of the first allocated frame.
     838 * @param pzone      Preferred zone.
    1016839 *
    1017840 * @return Physical address of the allocated frame.
    1018841 *
    1019842 */
    1020 void *frame_alloc_generic(uint8_t order, frame_flags_t flags, size_t *pzone)
    1021 {
    1022         size_t size = ((size_t) 1) << order;
     843uintptr_t frame_alloc_generic(size_t count, frame_flags_t flags,
     844    uintptr_t constraint, size_t *pzone)
     845{
     846        ASSERT(count > 0);
     847       
    1023848        size_t hint = pzone ? (*pzone) : 0;
     849        pfn_t frame_constraint = ADDR2PFN(constraint);
    1024850       
    1025851        /*
    1026852         * If not told otherwise, we must first reserve the memory.
    1027853         */
    1028         if (!(flags & FRAME_NO_RESERVE)) 
    1029                 reserve_force_alloc(size);
    1030 
     854        if (!(flags & FRAME_NO_RESERVE))
     855                reserve_force_alloc(count);
     856       
    1031857loop:
    1032858        irq_spinlock_lock(&zones.lock, true);
     
    1035861         * First, find suitable frame zone.
    1036862         */
    1037         size_t znum = find_free_zone(order,
    1038             FRAME_TO_ZONE_FLAGS(flags), hint);
    1039        
    1040         /* If no memory, reclaim some slab memory,
    1041            if it does not help, reclaim all */
     863        size_t znum = find_free_zone(count, FRAME_TO_ZONE_FLAGS(flags),
     864            frame_constraint, hint);
     865       
     866        /*
     867         * If no memory, reclaim some slab memory,
     868         * if it does not help, reclaim all.
     869         */
    1042870        if ((znum == (size_t) -1) && (!(flags & FRAME_NO_RECLAIM))) {
    1043871                irq_spinlock_unlock(&zones.lock, true);
     
    1046874               
    1047875                if (freed > 0)
    1048                         znum = find_free_zone(order,
    1049                             FRAME_TO_ZONE_FLAGS(flags), hint);
     876                        znum = find_free_zone(count, FRAME_TO_ZONE_FLAGS(flags),
     877                            frame_constraint, hint);
    1050878               
    1051879                if (znum == (size_t) -1) {
     
    1055883                       
    1056884                        if (freed > 0)
    1057                                 znum = find_free_zone(order,
    1058                                     FRAME_TO_ZONE_FLAGS(flags), hint);
     885                                znum = find_free_zone(count, FRAME_TO_ZONE_FLAGS(flags),
     886                                    frame_constraint, hint);
    1059887                }
    1060888        }
     
    1063891                if (flags & FRAME_ATOMIC) {
    1064892                        irq_spinlock_unlock(&zones.lock, true);
     893                       
    1065894                        if (!(flags & FRAME_NO_RESERVE))
    1066                                 reserve_free(size);
    1067                         return NULL;
     895                                reserve_free(count);
     896                       
     897                        return 0;
    1068898                }
    1069899               
     
    1075905               
    1076906                if (!THREAD)
    1077                         panic("Cannot wait for memory to become available.");
     907                        panic("Cannot wait for %zu frames to become available "
     908                            "(%zu available).", count, avail);
    1078909               
    1079910                /*
     
    1082913               
    1083914#ifdef CONFIG_DEBUG
    1084                 printf("Thread %" PRIu64 " waiting for %zu frames, "
    1085                     "%zu available.\n", THREAD->tid, size, avail);
     915                printf("Thread %" PRIu64 " waiting for %zu frames "
     916                    "(%zu available).\n", THREAD->tid, count, avail);
    1086917#endif
    1087918               
    1088919                /*
    1089                  * Since the mem_avail_mtx is an active mutex, we need to disable interrupts
    1090                  * to prevent deadlock with TLB shootdown.
     920                 * Since the mem_avail_mtx is an active mutex, we need to
     921                 * disable interrupts to prevent deadlock with TLB shootdown.
    1091922                 */
    1092923                ipl_t ipl = interrupts_disable();
     
    1094925               
    1095926                if (mem_avail_req > 0)
    1096                         mem_avail_req = min(mem_avail_req, size);
     927                        mem_avail_req = min(mem_avail_req, count);
    1097928                else
    1098                         mem_avail_req = size;
     929                        mem_avail_req = count;
     930               
    1099931                size_t gen = mem_avail_gen;
    1100932               
     
    1112944        }
    1113945       
    1114         pfn_t pfn = zone_frame_alloc(&zones.info[znum], order)
    1115             + zones.info[znum].base;
     946        pfn_t pfn = zone_frame_alloc(&zones.info[znum], count,
     947            frame_constraint) + zones.info[znum].base;
    1116948       
    1117949        irq_spinlock_unlock(&zones.lock, true);
     
    1120952                *pzone = znum;
    1121953       
    1122         if (flags & FRAME_KA)
    1123                 return (void *) PA2KA(PFN2ADDR(pfn));
    1124        
    1125         return (void *) PFN2ADDR(pfn);
    1126 }
    1127 
    1128 void *frame_alloc(uint8_t order, frame_flags_t flags)
    1129 {
    1130         return frame_alloc_generic(order, flags, NULL);
    1131 }
    1132 
    1133 void *frame_alloc_noreserve(uint8_t order, frame_flags_t flags)
    1134 {
    1135         return frame_alloc_generic(order, flags | FRAME_NO_RESERVE, NULL);
    1136 }
    1137 
    1138 /** Free a frame.
    1139  *
    1140  * Find respective frame structure for supplied physical frame address.
    1141  * Decrement frame reference count. If it drops to zero, move the frame
    1142  * structure to free list.
    1143  *
    1144  * @param frame Physical Address of of the frame to be freed.
     954        return PFN2ADDR(pfn);
     955}
     956
     957uintptr_t frame_alloc(size_t count, frame_flags_t flags, uintptr_t constraint)
     958{
     959        return frame_alloc_generic(count, flags, constraint, NULL);
     960}
     961
     962/** Free frames of physical memory.
     963 *
     964 * Find respective frame structures for supplied physical frames.
     965 * Decrement each frame reference count. If it drops to zero, mark
     966 * the frames as available.
     967 *
     968 * @param start Physical Address of the first frame to be freed.
     969 * @param count Number of frames to free.
    1145970 * @param flags Flags to control memory reservation.
    1146971 *
    1147972 */
    1148 void frame_free_generic(uintptr_t frame, frame_flags_t flags)
    1149 {
    1150         size_t size;
     973void frame_free_generic(uintptr_t start, size_t count, frame_flags_t flags)
     974{
     975        size_t freed = 0;
    1151976       
    1152977        irq_spinlock_lock(&zones.lock, true);
    1153978       
    1154         /*
    1155          * First, find host frame zone for addr.
    1156          */
    1157         pfn_t pfn = ADDR2PFN(frame);
    1158         size_t znum = find_zone(pfn, 1, 0);
    1159 
    1160         ASSERT(znum != (size_t) -1);
    1161        
    1162         size = zone_frame_free(&zones.info[znum], pfn - zones.info[znum].base);
     979        for (size_t i = 0; i < count; i++) {
     980                /*
     981                 * First, find host frame zone for addr.
     982                 */
     983                pfn_t pfn = ADDR2PFN(start) + i;
     984                size_t znum = find_zone(pfn, 1, 0);
     985               
     986                ASSERT(znum != (size_t) -1);
     987               
     988                freed += zone_frame_free(&zones.info[znum],
     989                    pfn - zones.info[znum].base);
     990        }
    1163991       
    1164992        irq_spinlock_unlock(&zones.lock, true);
     
    1166994        /*
    1167995         * Signal that some memory has been freed.
     996         * Since the mem_avail_mtx is an active mutex,
     997         * we need to disable interruptsto prevent deadlock
     998         * with TLB shootdown.
    1168999         */
    1169 
    1170        
    1171         /*
    1172          * Since the mem_avail_mtx is an active mutex, we need to disable interrupts
    1173          * to prevent deadlock with TLB shootdown.
    1174          */
     1000       
    11751001        ipl_t ipl = interrupts_disable();
    11761002        mutex_lock(&mem_avail_mtx);
     1003       
    11771004        if (mem_avail_req > 0)
    1178                 mem_avail_req -= min(mem_avail_req, size);
     1005                mem_avail_req -= min(mem_avail_req, freed);
    11791006       
    11801007        if (mem_avail_req == 0) {
     
    11821009                condvar_broadcast(&mem_avail_cv);
    11831010        }
     1011       
    11841012        mutex_unlock(&mem_avail_mtx);
    11851013        interrupts_restore(ipl);
    11861014       
    11871015        if (!(flags & FRAME_NO_RESERVE))
    1188                 reserve_free(size);
    1189 }
    1190 
    1191 void frame_free(uintptr_t frame)
    1192 {
    1193         frame_free_generic(frame, 0);
    1194 }
    1195 
    1196 void frame_free_noreserve(uintptr_t frame)
    1197 {
    1198         frame_free_generic(frame, FRAME_NO_RESERVE);
     1016                reserve_free(freed);
     1017}
     1018
     1019void frame_free(uintptr_t frame, size_t count)
     1020{
     1021        frame_free_generic(frame, count, 0);
     1022}
     1023
     1024void frame_free_noreserve(uintptr_t frame, size_t count)
     1025{
     1026        frame_free_generic(frame, count, FRAME_NO_RESERVE);
    11991027}
    12001028
     
    12301058        irq_spinlock_lock(&zones.lock, true);
    12311059       
    1232         size_t i;
    1233         for (i = 0; i < count; i++) {
     1060        for (size_t i = 0; i < count; i++) {
    12341061                size_t znum = find_zone(start + i, 1, 0);
     1062               
    12351063                if (znum == (size_t) -1)  /* PFN not found */
    12361064                        continue;
     
    12571085        /* Tell the architecture to create some memory */
    12581086        frame_low_arch_init();
     1087       
    12591088        if (config.cpu_active == 1) {
    12601089                frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)),
     
    12631092                    SIZE2FRAMES(config.stack_size));
    12641093               
    1265                 size_t i;
    1266                 for (i = 0; i < init.cnt; i++) {
    1267                         pfn_t pfn = ADDR2PFN(init.tasks[i].paddr);
    1268                         frame_mark_unavailable(pfn,
     1094                for (size_t i = 0; i < init.cnt; i++)
     1095                        frame_mark_unavailable(ADDR2PFN(init.tasks[i].paddr),
    12691096                            SIZE2FRAMES(init.tasks[i].size));
    1270                 }
    12711097               
    12721098                if (ballocs.size)
     
    12741100                            SIZE2FRAMES(ballocs.size));
    12751101               
    1276                 /* Black list first frame, as allocating NULL would
     1102                /*
     1103                 * Blacklist first frame, as allocating NULL would
    12771104                 * fail in some places
    12781105                 */
    12791106                frame_mark_unavailable(0, 1);
    12801107        }
     1108       
    12811109        frame_high_arch_init();
    12821110}
     
    12841112/** Adjust bounds of physical memory region according to low/high memory split.
    12851113 *
    1286  * @param low[in]       If true, the adjustment is performed to make the region
    1287  *                      fit in the low memory. Otherwise the adjustment is
    1288  *                      performed to make the region fit in the high memory.
    1289  * @param basep[inout]  Pointer to a variable which contains the region's base
    1290  *                      address and which may receive the adjusted base address.
    1291  * @param sizep[inout]  Pointer to a variable which contains the region's size
    1292  *                      and which may receive the adjusted size.
    1293  * @retun               True if the region still exists even after the
    1294  *                      adjustment, false otherwise.
     1114 * @param low[in]      If true, the adjustment is performed to make the region
     1115 *                     fit in the low memory. Otherwise the adjustment is
     1116 *                     performed to make the region fit in the high memory.
     1117 * @param basep[inout] Pointer to a variable which contains the region's base
     1118 *                     address and which may receive the adjusted base address.
     1119 * @param sizep[inout] Pointer to a variable which contains the region's size
     1120 *                     and which may receive the adjusted size.
     1121 *
     1122 * @return True if the region still exists even after the adjustment.
     1123 * @return False otherwise.
     1124 *
    12951125 */
    12961126bool frame_adjust_zone_bounds(bool low, uintptr_t *basep, size_t *sizep)
    12971127{
    12981128        uintptr_t limit = KA2PA(config.identity_base) + config.identity_size;
    1299 
     1129       
    13001130        if (low) {
    13011131                if (*basep > limit)
    13021132                        return false;
     1133               
    13031134                if (*basep + *sizep > limit)
    13041135                        *sizep = limit - *basep;
     
    13061137                if (*basep + *sizep <= limit)
    13071138                        return false;
     1139               
    13081140                if (*basep <= limit) {
    13091141                        *sizep -= limit - *basep;
     
    13111143                }
    13121144        }
     1145       
    13131146        return true;
    13141147}
     
    13221155       
    13231156        uint64_t total = 0;
    1324         size_t i;
    1325         for (i = 0; i < zones.count; i++)
     1157       
     1158        for (size_t i = 0; i < zones.count; i++)
    13261159                total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
    13271160       
     
    13461179        *free = 0;
    13471180       
    1348         size_t i;
    1349         for (i = 0; i < zones.count; i++) {
     1181        for (size_t i = 0; i < zones.count; i++) {
    13501182                *total += (uint64_t) FRAMES2SIZE(zones.info[i].count);
    13511183               
     
    13751207        /*
    13761208         * Because printing may require allocation of memory, we may not hold
    1377          * the frame allocator locks when printing zone statistics.  Therefore,
     1209         * the frame allocator locks when printing zone statistics. Therefore,
    13781210         * we simply gather the statistics under the protection of the locks and
    13791211         * print the statistics when the locks have been released.
     
    13841216         */
    13851217       
    1386         size_t i;
    1387         for (i = 0;; i++) {
     1218        size_t free_lowmem = 0;
     1219        size_t free_highmem = 0;
     1220        size_t free_highprio = 0;
     1221       
     1222        for (size_t i = 0;; i++) {
    13881223                irq_spinlock_lock(&zones.lock, true);
    13891224               
     
    13931228                }
    13941229               
    1395                 uintptr_t base = PFN2ADDR(zones.info[i].base);
     1230                pfn_t fbase = zones.info[i].base;
     1231                uintptr_t base = PFN2ADDR(fbase);
    13961232                size_t count = zones.info[i].count;
    13971233                zone_flags_t flags = zones.info[i].flags;
     
    13991235                size_t busy_count = zones.info[i].busy_count;
    14001236               
     1237                bool available = ((flags & ZONE_AVAILABLE) != 0);
     1238                bool lowmem = ((flags & ZONE_LOWMEM) != 0);
     1239                bool highmem = ((flags & ZONE_HIGHMEM) != 0);
     1240                bool highprio = is_high_priority(fbase, count);
     1241               
     1242                if (available) {
     1243                        if (lowmem)
     1244                                free_lowmem += free_count;
     1245                       
     1246                        if (highmem)
     1247                                free_highmem += free_count;
     1248                       
     1249                        if (highprio) {
     1250                                free_highprio += free_count;
     1251                        } else {
     1252                                /*
     1253                                 * Walk all frames of the zone and examine
     1254                                 * all high priority memory to get accurate
     1255                                 * statistics.
     1256                                 */
     1257                               
     1258                                for (size_t index = 0; index < count; index++) {
     1259                                        if (is_high_priority(fbase + index, 0)) {
     1260                                                if (!bitmap_get(&zones.info[i].bitmap, index))
     1261                                                        free_highprio++;
     1262                                        } else
     1263                                                break;
     1264                                }
     1265                        }
     1266                }
     1267               
    14011268                irq_spinlock_unlock(&zones.lock, true);
    1402                
    1403                 bool available = ((flags & ZONE_AVAILABLE) != 0);
    14041269               
    14051270                printf("%-4zu", i);
     
    14261291                printf("\n");
    14271292        }
     1293       
     1294        printf("\n");
     1295       
     1296        uint64_t size;
     1297        const char *size_suffix;
     1298       
     1299        bin_order_suffix(FRAMES2SIZE(free_lowmem), &size, &size_suffix,
     1300            false);
     1301        printf("Available low memory:    %zu frames (%" PRIu64 " %s)\n",
     1302            free_lowmem, size, size_suffix);
     1303       
     1304        bin_order_suffix(FRAMES2SIZE(free_highmem), &size, &size_suffix,
     1305            false);
     1306        printf("Available high memory:   %zu frames (%" PRIu64 " %s)\n",
     1307            free_highmem, size, size_suffix);
     1308       
     1309        bin_order_suffix(FRAMES2SIZE(free_highprio), &size, &size_suffix,
     1310            false);
     1311        printf("Available high priority: %zu frames (%" PRIu64 " %s)\n",
     1312            free_highprio, size, size_suffix);
    14281313}
    14291314
     
    14381323        size_t znum = (size_t) -1;
    14391324       
    1440         size_t i;
    1441         for (i = 0; i < zones.count; i++) {
     1325        for (size_t i = 0; i < zones.count; i++) {
    14421326                if ((i == num) || (PFN2ADDR(zones.info[i].base) == num)) {
    14431327                        znum = i;
     
    14521336        }
    14531337       
    1454         uintptr_t base = PFN2ADDR(zones.info[i].base);
    1455         zone_flags_t flags = zones.info[i].flags;
    1456         size_t count = zones.info[i].count;
    1457         size_t free_count = zones.info[i].free_count;
    1458         size_t busy_count = zones.info[i].busy_count;
     1338        size_t free_lowmem = 0;
     1339        size_t free_highmem = 0;
     1340        size_t free_highprio = 0;
     1341       
     1342        pfn_t fbase = zones.info[znum].base;
     1343        uintptr_t base = PFN2ADDR(fbase);
     1344        zone_flags_t flags = zones.info[znum].flags;
     1345        size_t count = zones.info[znum].count;
     1346        size_t free_count = zones.info[znum].free_count;
     1347        size_t busy_count = zones.info[znum].busy_count;
     1348       
     1349        bool available = ((flags & ZONE_AVAILABLE) != 0);
     1350        bool lowmem = ((flags & ZONE_LOWMEM) != 0);
     1351        bool highmem = ((flags & ZONE_HIGHMEM) != 0);
     1352        bool highprio = is_high_priority(fbase, count);
     1353       
     1354        if (available) {
     1355                if (lowmem)
     1356                        free_lowmem = free_count;
     1357               
     1358                if (highmem)
     1359                        free_highmem = free_count;
     1360               
     1361                if (highprio) {
     1362                        free_highprio = free_count;
     1363                } else {
     1364                        /*
     1365                         * Walk all frames of the zone and examine
     1366                         * all high priority memory to get accurate
     1367                         * statistics.
     1368                         */
     1369                       
     1370                        for (size_t index = 0; index < count; index++) {
     1371                                if (is_high_priority(fbase + index, 0)) {
     1372                                        if (!bitmap_get(&zones.info[znum].bitmap, index))
     1373                                                free_highprio++;
     1374                                } else
     1375                                        break;
     1376                        }
     1377                }
     1378        }
    14591379       
    14601380        irq_spinlock_unlock(&zones.lock, true);
    1461        
    1462         bool available = ((flags & ZONE_AVAILABLE) != 0);
    14631381       
    14641382        uint64_t size;
    14651383        const char *size_suffix;
     1384       
    14661385        bin_order_suffix(FRAMES2SIZE(count), &size, &size_suffix, false);
    14671386       
    1468         printf("Zone number:       %zu\n", znum);
    1469         printf("Zone base address: %p\n", (void *) base);
    1470         printf("Zone size:         %zu frames (%" PRIu64 " %s)\n", count,
     1387        printf("Zone number:             %zu\n", znum);
     1388        printf("Zone base address:       %p\n", (void *) base);
     1389        printf("Zone size:               %zu frames (%" PRIu64 " %s)\n", count,
    14711390            size, size_suffix);
    1472         printf("Zone flags:        %c%c%c%c%c\n",
     1391        printf("Zone flags:              %c%c%c%c%c\n",
    14731392            available ? 'A' : '-',
    14741393            (flags & ZONE_RESERVED) ? 'R' : '-',
     
    14801399                bin_order_suffix(FRAMES2SIZE(busy_count), &size, &size_suffix,
    14811400                    false);
    1482                 printf("Allocated space:   %zu frames (%" PRIu64 " %s)\n",
     1401                printf("Allocated space:         %zu frames (%" PRIu64 " %s)\n",
    14831402                    busy_count, size, size_suffix);
     1403               
    14841404                bin_order_suffix(FRAMES2SIZE(free_count), &size, &size_suffix,
    14851405                    false);
    1486                 printf("Available space:   %zu frames (%" PRIu64 " %s)\n",
     1406                printf("Available space:         %zu frames (%" PRIu64 " %s)\n",
    14871407                    free_count, size, size_suffix);
     1408               
     1409                bin_order_suffix(FRAMES2SIZE(free_lowmem), &size, &size_suffix,
     1410                    false);
     1411                printf("Available low memory:    %zu frames (%" PRIu64 " %s)\n",
     1412                    free_lowmem, size, size_suffix);
     1413               
     1414                bin_order_suffix(FRAMES2SIZE(free_highmem), &size, &size_suffix,
     1415                    false);
     1416                printf("Available high memory:   %zu frames (%" PRIu64 " %s)\n",
     1417                    free_highmem, size, size_suffix);
     1418               
     1419                bin_order_suffix(FRAMES2SIZE(free_highprio), &size, &size_suffix,
     1420                    false);
     1421                printf("Available high priority: %zu frames (%" PRIu64 " %s)\n",
     1422                    free_highprio, size, size_suffix);
    14881423        }
    14891424}
  • kernel/generic/src/mm/km.c

    r3deb0155 rdd0c8a0  
    239239uintptr_t km_temporary_page_get(uintptr_t *framep, frame_flags_t flags)
    240240{
    241         uintptr_t frame;
    242         uintptr_t page;
    243 
    244241        ASSERT(THREAD);
    245242        ASSERT(framep);
    246243        ASSERT(!(flags & ~(FRAME_NO_RESERVE | FRAME_ATOMIC)));
    247 
     244       
    248245        /*
    249246         * Allocate a frame, preferably from high memory.
    250247         */
    251         frame = (uintptr_t) frame_alloc(ONE_FRAME,
    252             FRAME_HIGHMEM | FRAME_ATOMIC | flags);
     248        uintptr_t page;
     249        uintptr_t frame =
     250            frame_alloc(1, FRAME_HIGHMEM | FRAME_ATOMIC | flags, 0);
    253251        if (frame) {
    254252                page = km_map(frame, PAGE_SIZE,
    255253                    PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
    256                 ASSERT(page);   // FIXME
     254               
     255                // FIXME
     256                ASSERT(page);
    257257        } else {
    258                 frame = (uintptr_t) frame_alloc(ONE_FRAME,
    259                     FRAME_LOWMEM | flags);
     258                frame = frame_alloc(1, FRAME_LOWMEM | flags, 0);
    260259                if (!frame)
    261260                        return (uintptr_t) NULL;
     261               
    262262                page = PA2KA(frame);
    263263        }
    264 
     264       
    265265        *framep = frame;
    266         return page;   
     266        return page;
    267267}
    268268
  • kernel/generic/src/mm/page.c

    r3deb0155 rdd0c8a0  
    169169}
    170170
    171 int page_find_mapping(uintptr_t virt, void **phys)
     171int page_find_mapping(uintptr_t virt, uintptr_t *phys)
    172172{
    173173        page_table_lock(AS, true);
     
    179179        }
    180180       
    181         *phys = (void *) PTE_GET_FRAME(pte) +
     181        *phys = PTE_GET_FRAME(pte) +
    182182            (virt - ALIGN_DOWN(virt, PAGE_SIZE));
    183183       
     
    193193 *
    194194 */
    195 sysarg_t sys_page_find_mapping(uintptr_t virt, void *phys_ptr)
    196 {
    197         void *phys;
     195sysarg_t sys_page_find_mapping(uintptr_t virt, uintptr_t *phys_ptr)
     196{
     197        uintptr_t phys;
    198198        int rc = page_find_mapping(virt, &phys);
    199199        if (rc != EOK)
  • kernel/generic/src/mm/slab.c

    r3deb0155 rdd0c8a0  
    182182        size_t zone = 0;
    183183       
    184         void *data = frame_alloc_generic(cache->order, FRAME_KA | flags, &zone);
    185         if (!data) {
     184        uintptr_t data_phys =
     185            frame_alloc_generic(cache->frames, flags, 0, &zone);
     186        if (!data_phys)
    186187                return NULL;
    187         }
     188       
     189        void *data = (void *) PA2KA(data_phys);
    188190       
    189191        slab_t *slab;
     
    193195                slab = slab_alloc(slab_extern_cache, flags);
    194196                if (!slab) {
    195                         frame_free(KA2PA(data));
     197                        frame_free(KA2PA(data), cache->frames);
    196198                        return NULL;
    197199                }
    198200        } else {
    199                 fsize = (PAGE_SIZE << cache->order);
     201                fsize = FRAMES2SIZE(cache->frames);
    200202                slab = data + fsize - sizeof(*slab);
    201203        }
     
    203205        /* Fill in slab structures */
    204206        size_t i;
    205         for (i = 0; i < ((size_t) 1 << cache->order); i++)
     207        for (i = 0; i < cache->frames; i++)
    206208                frame_set_parent(ADDR2PFN(KA2PA(data)) + i, slab, zone);
    207209       
     
    225227NO_TRACE static size_t slab_space_free(slab_cache_t *cache, slab_t *slab)
    226228{
    227         frame_free(KA2PA(slab->start));
     229        frame_free(KA2PA(slab->start), slab->cache->frames);
    228230        if (!(cache->flags & SLAB_CACHE_SLINSIDE))
    229231                slab_free(slab_extern_cache, slab);
     
    231233        atomic_dec(&cache->allocated_slabs);
    232234       
    233         return (1 << cache->order);
     235        return cache->frames;
    234236}
    235237
     
    558560{
    559561        if (cache->flags & SLAB_CACHE_SLINSIDE)
    560                 return ((PAGE_SIZE << cache->order)
    561                     - sizeof(slab_t)) / cache->size;
     562                return (FRAMES2SIZE(cache->frames) - sizeof(slab_t)) /
     563                    cache->size;
    562564        else
    563                 return (PAGE_SIZE << cache->order) / cache->size;
     565                return FRAMES2SIZE(cache->frames) / cache->size;
    564566}
    565567
     
    570572{
    571573        size_t objects = comp_objects(cache);
    572         size_t ssize = PAGE_SIZE << cache->order;
     574        size_t ssize = FRAMES2SIZE(cache->frames);
    573575       
    574576        if (cache->flags & SLAB_CACHE_SLINSIDE)
     
    634636                cache->flags |= SLAB_CACHE_SLINSIDE;
    635637       
    636         /* Minimum slab order */
    637         size_t pages = SIZE2FRAMES(cache->size);
    638        
    639         /* We need the 2^order >= pages */
    640         if (pages == 1)
    641                 cache->order = 0;
    642         else
    643                 cache->order = fnzb(pages - 1) + 1;
     638        /* Minimum slab frames */
     639        cache->frames = SIZE2FRAMES(cache->size);
    644640       
    645641        while (badness(cache) > SLAB_MAX_BADNESS(cache))
    646                 cache->order += 1;
     642                cache->frames <<= 1;
    647643       
    648644        cache->objects = comp_objects(cache);
     
    810806       
    811807        size_t frames = 0;
    812         list_foreach(slab_cache_list, cur) {
    813                 slab_cache_t *cache = list_get_instance(cur, slab_cache_t, link);
     808        list_foreach(slab_cache_list, link, slab_cache_t, cache) {
    814809                frames += _slab_reclaim(cache, flags);
    815810        }
     
    871866               
    872867                const char *name = cache->name;
    873                 uint8_t order = cache->order;
     868                size_t frames = cache->frames;
    874869                size_t size = cache->size;
    875870                size_t objects = cache->objects;
     
    881876                irq_spinlock_unlock(&slab_cache_lock, true);
    882877               
    883                 printf("%-18s %8zu %8u %8zu %8ld %8ld %8ld %-5s\n",
    884                     name, size, (1 << order), objects, allocated_slabs,
     878                printf("%-18s %8zu %8zu %8zu %8ld %8ld %8ld %-5s\n",
     879                    name, size, frames, objects, allocated_slabs,
    885880                    cached_objs, allocated_objs,
    886881                    flags & SLAB_CACHE_SLINSIDE ? "in" : "out");
     
    936931        irq_spinlock_lock(&slab_cache_lock, false);
    937932       
    938         list_foreach(slab_cache_list, cur) {
    939                 slab_cache_t *slab = list_get_instance(cur, slab_cache_t, link);
     933        list_foreach(slab_cache_list, link, slab_cache_t, slab) {
    940934                if ((slab->flags & SLAB_CACHE_MAGDEFERRED) !=
    941935                    SLAB_CACHE_MAGDEFERRED)
  • kernel/generic/src/proc/scheduler.c

    r3deb0155 rdd0c8a0  
    739739                       
    740740                        printf("\trq[%u]: ", i);
    741                         list_foreach(cpus[cpu].rq[i].rq, cur) {
    742                                 thread_t *thread = list_get_instance(cur,
    743                                     thread_t, rq_link);
     741                        list_foreach(cpus[cpu].rq[i].rq, rq_link, thread_t,
     742                            thread) {
    744743                                printf("%" PRIu64 "(%s) ", thread->tid,
    745744                                    thread_states[thread->state]);
  • kernel/generic/src/proc/task.c

    r3deb0155 rdd0c8a0  
    452452       
    453453        /* Current values of threads */
    454         list_foreach(task->threads, cur) {
    455                 thread_t *thread = list_get_instance(cur, thread_t, th_link);
    456                
     454        list_foreach(task->threads, th_link, thread_t, thread) {
    457455                irq_spinlock_lock(&thread->lock, false);
    458456               
     
    484482         */
    485483       
    486         list_foreach(task->threads, cur) {
    487                 thread_t *thread = list_get_instance(cur, thread_t, th_link);
     484        list_foreach(task->threads, th_link, thread_t, thread) {
    488485                bool sleeping = false;
    489486               
  • kernel/generic/src/proc/thread.c

    r3deb0155 rdd0c8a0  
    192192        kmflags &= ~FRAME_HIGHMEM;
    193193       
    194         thread->kstack = (uint8_t *) frame_alloc(STACK_FRAMES, FRAME_KA | kmflags);
    195         if (!thread->kstack) {
     194        uintptr_t stack_phys =
     195            frame_alloc(STACK_FRAMES, kmflags, STACK_SIZE - 1);
     196        if (!stack_phys) {
    196197#ifdef CONFIG_FPU
    197198                if (thread->saved_fpu_context)
     
    201202        }
    202203       
     204        thread->kstack = (uint8_t *) PA2KA(stack_phys);
     205       
    203206#ifdef CONFIG_UDEBUG
    204207        mutex_initialize(&thread->udebug.lock, MUTEX_PASSIVE);
     
    216219        thr_destructor_arch(thread);
    217220       
    218         frame_free(KA2PA(thread->kstack));
     221        frame_free(KA2PA(thread->kstack), STACK_FRAMES);
    219222       
    220223#ifdef CONFIG_FPU
  • kernel/generic/src/synch/futex.c

    r3deb0155 rdd0c8a0  
    274274        mutex_lock(&TASK->futexes_lock);
    275275
    276         list_foreach(TASK->futexes.leaf_list, cur) {
    277                 btree_node_t *node;
     276        list_foreach(TASK->futexes.leaf_list, leaf_link, btree_node_t, node) {
    278277                unsigned int i;
    279278               
    280                 node = list_get_instance(cur, btree_node_t, leaf_link);
    281279                for (i = 0; i < node->keys; i++) {
    282280                        futex_t *ftx;
  • kernel/generic/src/sysinfo/stats.c

    r3deb0155 rdd0c8a0  
    175175       
    176176        /* Walk the B+ tree and count pages */
    177         list_foreach(as->as_area_btree.leaf_list, cur) {
    178                 btree_node_t *node =
    179                     list_get_instance(cur, btree_node_t, leaf_link);
    180                
     177        list_foreach(as->as_area_btree.leaf_list, leaf_link, btree_node_t,
     178            node) {
    181179                unsigned int i;
    182180                for (i = 0; i < node->keys; i++) {
     
    218216       
    219217        /* Walk the B+ tree and count pages */
    220         list_foreach(as->as_area_btree.leaf_list, cur) {
    221                 btree_node_t *node =
    222                     list_get_instance(cur, btree_node_t, leaf_link);
    223                
     218        list_foreach(as->as_area_btree.leaf_list, leaf_link, btree_node_t, node) {
    224219                unsigned int i;
    225220                for (i = 0; i < node->keys; i++) {
  • kernel/generic/src/time/clock.c

    r3deb0155 rdd0c8a0  
    8181void clock_counter_init(void)
    8282{
    83         void *faddr = frame_alloc(ONE_FRAME, FRAME_ATOMIC);
    84         if (!faddr)
     83        uintptr_t faddr = frame_alloc(1, FRAME_ATOMIC, 0);
     84        if (faddr == 0)
    8585                panic("Cannot allocate page for clock.");
    8686       
     
    9191        uptime->useconds = 0;
    9292       
    93         clock_parea.pbase = (uintptr_t) faddr;
     93        clock_parea.pbase = faddr;
    9494        clock_parea.frames = 1;
    9595        clock_parea.unpriv = true;
  • kernel/generic/src/udebug/udebug.c

    r3deb0155 rdd0c8a0  
    406406       
    407407        /* Finish debugging of all userspace threads */
    408         list_foreach(task->threads, cur) {
    409                 thread_t *thread = list_get_instance(cur, thread_t, th_link);
    410                
     408        list_foreach(task->threads, th_link, thread_t, thread) {
    411409                mutex_lock(&thread->udebug.lock);
    412410               
  • kernel/generic/src/udebug/udebug_ops.c

    r3deb0155 rdd0c8a0  
    196196        /* Set udebug.active on all of the task's userspace threads. */
    197197       
    198         list_foreach(TASK->threads, cur) {
    199                 thread_t *thread = list_get_instance(cur, thread_t, th_link);
    200                
     198        list_foreach(TASK->threads, th_link, thread_t, thread) {
    201199                mutex_lock(&thread->udebug.lock);
    202200                if (thread->uspace) {
     
    389387       
    390388        /* FIXME: make sure the thread isn't past debug shutdown... */
    391         list_foreach(TASK->threads, cur) {
    392                 thread_t *thread = list_get_instance(cur, thread_t, th_link);
    393                
     389        list_foreach(TASK->threads, th_link, thread_t, thread) {
    394390                irq_spinlock_lock(&thread->lock, false);
    395391                bool uspace = thread->uspace;
Note: See TracChangeset for help on using the changeset viewer.