summaryrefslogtreecommitdiff
path: root/lume-legacy/src/kernel/bio.cc
blob: 5f1aae9f5809dd208ef8328d90a4c4b4b41b6921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2026 Richard QIn
 */
#include "kernel/buf.h"
#include "kernel/spinlock.h"
#include "kernel/riscv.h"
#include "drivers/virtio.h"
#include "drivers/uart.h"

namespace BufferCache
{
    struct 
    {
        Spinlock lock;
        struct Buf buf[NBUF];
        struct Buf head;
    } bcache;

    void init()
    {
        bcache.lock.init("bcache");
        // Initialize LRU doubly circular linked list
        bcache.head.prev = &bcache.head;
        bcache.head.next = &bcache.head;
        for (struct Buf *b = bcache.buf; b < bcache.buf + NBUF; ++b)
        {
            b->lock.init("buffer");
            b->next = bcache.head.next;
            b->prev = &bcache.head;
            bcache.head.next->prev = b;
            bcache.head.next = b;
        }
        Drivers::uart_puts("[Bio] Buffer cache initialized.\n");
    }

    static struct Buf* bget(uint32 dev, uint32 blockno)
    {
        struct Buf* b;
        bcache.lock.acquire();

        // Attempt to hit the cache
        for (b = bcache.head.next; b != &bcache.head; b = b->next)
        {
            if (b->dev == dev && b->blockno == blockno)
            {
                b->refcnt++;
                bcache.lock.release();
                b->lock.acquire(); // Acquire Sleep Lock
                return b;
            }
        }

        // Cache miss, evicting LRU
        for (b = bcache.head.prev; b != &bcache.head; b = b->prev)
        {
            if (b->refcnt == 0)
            {
                b->dev = dev;
                b->blockno = blockno;
                b->valid = 0;
                b->refcnt = 1;
                bcache.lock.release();
                b->lock.acquire();
                return b;
            }
        }

        Drivers::uart_puts("panic: bget: no buffers\n");
        while (1)
            ;
    }

    struct Buf* bread(uint32 dev, uint32 blockno)
    {
        struct Buf *b = bget(dev, blockno);

        if (!b->valid)
        {
            VirtIO::rw(0, b->blockno, (char *)b->data);
            b->valid = 1;
        }

        return b;
    }

    void bwrite(struct Buf *b)
    {
        if (!b->lock.holding())
        { 
            Drivers::uart_puts("panic: bwrite\n");
            while (1)
                ;
        }
        VirtIO::rw(1, b->blockno, (char *)b->data);
    }

    void brelse(struct Buf *b)
    {
        if (!b->lock.holding())
        {
            Drivers::uart_puts("panic: brelse\n");
            while (1)
                ;
        }
        b->lock.release();

        bcache.lock.acquire();
        b->refcnt--;

        if (b->refcnt == 0)
        {
            b->next->prev = b->prev;
            b->prev->next = b->next;

            b->next = bcache.head.next;
            b->prev = &bcache.head;
            bcache.head.next->prev = b;
            bcache.head.next = b;
        }

        bcache.lock.release();
    }

    void bpin(struct Buf *b)
    {
        bcache.lock.acquire();
        b->refcnt++;
        bcache.lock.release();
    }

    void bunpin(struct Buf* b)
    {
        bcache.lock.acquire();
        b->refcnt --;
        bcache.lock.release();
    }

} // namespace BufferCache