fix: format + dont track build folders
This commit is contained in:
parent
b77ad042ca
commit
c173f2b9b8
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
kern/compile/
|
||||
build/
|
||||
build/*
|
||||
**/build
|
||||
|
@ -92,18 +92,9 @@ void spllower(int oldipl, int newipl);
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
SPL_INLINE
|
||||
int
|
||||
spl0(void)
|
||||
{
|
||||
return splx(IPL_NONE);
|
||||
}
|
||||
int spl0(void) { return splx(IPL_NONE); }
|
||||
|
||||
SPL_INLINE
|
||||
int
|
||||
splhigh(void)
|
||||
{
|
||||
return splx(IPL_HIGH);
|
||||
}
|
||||
|
||||
int splhigh(void) { return splx(IPL_HIGH); }
|
||||
|
||||
#endif /* _SPL_H_ */
|
||||
|
@ -34,7 +34,6 @@
|
||||
* Header file for synchronization primitives.
|
||||
*/
|
||||
|
||||
|
||||
#include <spinlock.h>
|
||||
|
||||
/*
|
||||
@ -62,7 +61,6 @@ void sem_destroy(struct semaphore *);
|
||||
void P(struct semaphore *);
|
||||
void V(struct semaphore *);
|
||||
|
||||
|
||||
/*
|
||||
* Simple lock for mutual exclusion.
|
||||
*
|
||||
@ -97,7 +95,6 @@ void lock_acquire(struct lock *);
|
||||
void lock_release(struct lock *);
|
||||
bool lock_do_i_hold(struct lock *);
|
||||
|
||||
|
||||
/*
|
||||
* Condition variable.
|
||||
*
|
||||
@ -138,5 +135,4 @@ void cv_wait(struct cv *cv, struct lock *lock);
|
||||
void cv_signal(struct cv *cv, struct lock *lock);
|
||||
void cv_broadcast(struct cv *cv, struct lock *lock);
|
||||
|
||||
|
||||
#endif /* _SYNCH_H_ */
|
||||
|
@ -40,7 +40,6 @@
|
||||
#include <vfs.h> // for vfs_sync()
|
||||
#include <lamebus/ltrace.h> // for ltrace_stop()
|
||||
|
||||
|
||||
/* Flags word for DEBUG() macro. */
|
||||
uint32_t dbflags = 0;
|
||||
|
||||
@ -50,20 +49,16 @@ static struct lock *kprintf_lock;
|
||||
/* Lock for polled kprintfs */
|
||||
static struct spinlock kprintf_spinlock;
|
||||
|
||||
|
||||
/*
|
||||
* Warning: all this has to work from interrupt handlers and when
|
||||
* interrupts are disabled.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Create the kprintf lock. Must be called before creating a second
|
||||
* thread or enabling a second CPU.
|
||||
*/
|
||||
void
|
||||
kprintf_bootstrap(void)
|
||||
{
|
||||
void kprintf_bootstrap(void) {
|
||||
KASSERT(kprintf_lock == NULL);
|
||||
|
||||
kprintf_lock = lock_create("kprintf_lock");
|
||||
@ -76,15 +71,12 @@ kprintf_bootstrap(void)
|
||||
/*
|
||||
* Send characters to the console. Backend for __printf.
|
||||
*/
|
||||
static
|
||||
void
|
||||
console_send(void *junk, const char *data, size_t len)
|
||||
{
|
||||
static void console_send(void *junk, const char *data, size_t len) {
|
||||
size_t i;
|
||||
|
||||
(void)junk;
|
||||
|
||||
for (i=0; i<len; i++) {
|
||||
for (i = 0; i < len; i++) {
|
||||
putch(data[i]);
|
||||
}
|
||||
}
|
||||
@ -92,22 +84,17 @@ console_send(void *junk, const char *data, size_t len)
|
||||
/*
|
||||
* Printf to the console.
|
||||
*/
|
||||
int
|
||||
kprintf(const char *fmt, ...)
|
||||
{
|
||||
int kprintf(const char *fmt, ...) {
|
||||
int chars;
|
||||
va_list ap;
|
||||
bool dolock;
|
||||
|
||||
dolock = kprintf_lock != NULL
|
||||
&& curthread->t_in_interrupt == false
|
||||
&& curthread->t_curspl == 0
|
||||
&& curcpu->c_spinlocks == 0;
|
||||
dolock = kprintf_lock != NULL && curthread->t_in_interrupt == false &&
|
||||
curthread->t_curspl == 0 && curcpu->c_spinlocks == 0;
|
||||
|
||||
if (dolock) {
|
||||
lock_acquire(kprintf_lock);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
spinlock_acquire(&kprintf_spinlock);
|
||||
}
|
||||
|
||||
@ -117,8 +104,7 @@ kprintf(const char *fmt, ...)
|
||||
|
||||
if (dolock) {
|
||||
lock_release(kprintf_lock);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
spinlock_release(&kprintf_spinlock);
|
||||
}
|
||||
|
||||
@ -130,9 +116,7 @@ kprintf(const char *fmt, ...)
|
||||
* passed and then halts the system.
|
||||
*/
|
||||
|
||||
void
|
||||
panic(const char *fmt, ...)
|
||||
{
|
||||
void panic(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
|
||||
/*
|
||||
@ -202,15 +186,13 @@ panic(const char *fmt, ...)
|
||||
* Last resort, just in case.
|
||||
*/
|
||||
|
||||
for (;;);
|
||||
for (;;)
|
||||
;
|
||||
}
|
||||
|
||||
/*
|
||||
* Assertion failures go through this.
|
||||
*/
|
||||
void
|
||||
badassert(const char *expr, const char *file, int line, const char *func)
|
||||
{
|
||||
panic("Assertion failed: %s, at %s:%d (%s)\n",
|
||||
expr, file, line, func);
|
||||
void badassert(const char *expr, const char *file, int line, const char *func) {
|
||||
panic("Assertion failed: %s, at %s:%d (%s)\n", expr, file, line, func);
|
||||
}
|
||||
|
@ -51,7 +51,6 @@
|
||||
#include <version.h>
|
||||
#include "autoconf.h" // for pseudoconfig
|
||||
|
||||
|
||||
/*
|
||||
* These two pieces of data are maintained by the makefiles and build system.
|
||||
* buildconfig is the name of the config file the kernel was configured with.
|
||||
@ -71,14 +70,10 @@ static const char harvard_copyright[] =
|
||||
"Copyright (c) 2000, 2001-2005, 2008-2011, 2013, 2014\n"
|
||||
" President and Fellows of Harvard College. All rights reserved.\n";
|
||||
|
||||
|
||||
/*
|
||||
* Initial boot sequence.
|
||||
*/
|
||||
static
|
||||
void
|
||||
boot(void)
|
||||
{
|
||||
static void boot(void) {
|
||||
/*
|
||||
* The order of these is important!
|
||||
* Don't go changing it without thinking about the consequences.
|
||||
@ -101,8 +96,8 @@ boot(void)
|
||||
kprintf("%s", harvard_copyright);
|
||||
kprintf("\n");
|
||||
|
||||
kprintf("Put-your-group-name-here's system version %s (%s #%d)\n",
|
||||
GROUP_VERSION, buildconfig, buildversion);
|
||||
kprintf("Minh Tran's system version %s (%s #%d)\n", GROUP_VERSION,
|
||||
buildconfig, buildversion);
|
||||
kprintf("\n");
|
||||
|
||||
/* Early initialization. */
|
||||
@ -143,10 +138,7 @@ boot(void)
|
||||
/*
|
||||
* Shutdown sequence. Opposite to boot().
|
||||
*/
|
||||
static
|
||||
void
|
||||
shutdown(void)
|
||||
{
|
||||
static void shutdown(void) {
|
||||
|
||||
kprintf("Shutting down.\n");
|
||||
|
||||
@ -168,9 +160,7 @@ shutdown(void)
|
||||
* not because this is where system call code should go. Other syscall
|
||||
* code should probably live in the "syscall" directory.
|
||||
*/
|
||||
int
|
||||
sys_reboot(int code)
|
||||
{
|
||||
int sys_reboot(int code) {
|
||||
switch (code) {
|
||||
case RB_REBOOT:
|
||||
case RB_HALT:
|
||||
@ -205,9 +195,7 @@ sys_reboot(int code)
|
||||
* Kernel main. Boot up, then fork the menu thread; wait for a reboot
|
||||
* request, and then shut down.
|
||||
*/
|
||||
void
|
||||
kmain(char *arguments)
|
||||
{
|
||||
void kmain(char *arguments) {
|
||||
boot();
|
||||
|
||||
menu(arguments);
|
||||
|
@ -44,9 +44,7 @@
|
||||
//
|
||||
// Semaphore.
|
||||
|
||||
struct semaphore *
|
||||
sem_create(const char *name, unsigned initial_count)
|
||||
{
|
||||
struct semaphore *sem_create(const char *name, unsigned initial_count) {
|
||||
struct semaphore *sem;
|
||||
|
||||
sem = kmalloc(sizeof(*sem));
|
||||
@ -73,9 +71,7 @@ sem_create(const char *name, unsigned initial_count)
|
||||
return sem;
|
||||
}
|
||||
|
||||
void
|
||||
sem_destroy(struct semaphore *sem)
|
||||
{
|
||||
void sem_destroy(struct semaphore *sem) {
|
||||
KASSERT(sem != NULL);
|
||||
|
||||
/* wchan_cleanup will assert if anyone's waiting on it */
|
||||
@ -85,9 +81,7 @@ sem_destroy(struct semaphore *sem)
|
||||
kfree(sem);
|
||||
}
|
||||
|
||||
void
|
||||
P(struct semaphore *sem)
|
||||
{
|
||||
void P(struct semaphore *sem) {
|
||||
KASSERT(sem != NULL);
|
||||
|
||||
/*
|
||||
@ -120,9 +114,7 @@ P(struct semaphore *sem)
|
||||
spinlock_release(&sem->sem_lock);
|
||||
}
|
||||
|
||||
void
|
||||
V(struct semaphore *sem)
|
||||
{
|
||||
void V(struct semaphore *sem) {
|
||||
KASSERT(sem != NULL);
|
||||
|
||||
spinlock_acquire(&sem->sem_lock);
|
||||
@ -138,9 +130,7 @@ V(struct semaphore *sem)
|
||||
//
|
||||
// Lock.
|
||||
|
||||
struct lock *
|
||||
lock_create(const char *name)
|
||||
{
|
||||
struct lock *lock_create(const char *name) {
|
||||
struct lock *lock;
|
||||
|
||||
lock = kmalloc(sizeof(*lock));
|
||||
@ -161,9 +151,7 @@ lock_create(const char *name)
|
||||
return lock;
|
||||
}
|
||||
|
||||
void
|
||||
lock_destroy(struct lock *lock)
|
||||
{
|
||||
void lock_destroy(struct lock *lock) {
|
||||
KASSERT(lock != NULL);
|
||||
|
||||
// add stuff here as needed
|
||||
@ -172,34 +160,28 @@ lock_destroy(struct lock *lock)
|
||||
kfree(lock);
|
||||
}
|
||||
|
||||
void
|
||||
lock_acquire(struct lock *lock)
|
||||
{
|
||||
void lock_acquire(struct lock *lock) {
|
||||
/* Call this (atomically) before waiting for a lock */
|
||||
//HANGMAN_WAIT(&curthread->t_hangman, &lock->lk_hangman);
|
||||
// HANGMAN_WAIT(&curthread->t_hangman, &lock->lk_hangman);
|
||||
|
||||
// Write this
|
||||
|
||||
(void)lock; // suppress warning until code gets written
|
||||
|
||||
/* Call this (atomically) once the lock is acquired */
|
||||
//HANGMAN_ACQUIRE(&curthread->t_hangman, &lock->lk_hangman);
|
||||
// HANGMAN_ACQUIRE(&curthread->t_hangman, &lock->lk_hangman);
|
||||
}
|
||||
|
||||
void
|
||||
lock_release(struct lock *lock)
|
||||
{
|
||||
void lock_release(struct lock *lock) {
|
||||
/* Call this (atomically) when the lock is released */
|
||||
//HANGMAN_RELEASE(&curthread->t_hangman, &lock->lk_hangman);
|
||||
// HANGMAN_RELEASE(&curthread->t_hangman, &lock->lk_hangman);
|
||||
|
||||
// Write this
|
||||
|
||||
(void)lock; // suppress warning until code gets written
|
||||
}
|
||||
|
||||
bool
|
||||
lock_do_i_hold(struct lock *lock)
|
||||
{
|
||||
bool lock_do_i_hold(struct lock *lock) {
|
||||
// Write this
|
||||
|
||||
(void)lock; // suppress warning until code gets written
|
||||
@ -211,10 +193,7 @@ lock_do_i_hold(struct lock *lock)
|
||||
//
|
||||
// CV
|
||||
|
||||
|
||||
struct cv *
|
||||
cv_create(const char *name)
|
||||
{
|
||||
struct cv *cv_create(const char *name) {
|
||||
struct cv *cv;
|
||||
|
||||
cv = kmalloc(sizeof(*cv));
|
||||
@ -223,7 +202,7 @@ cv_create(const char *name)
|
||||
}
|
||||
|
||||
cv->cv_name = kstrdup(name);
|
||||
if (cv->cv_name==NULL) {
|
||||
if (cv->cv_name == NULL) {
|
||||
kfree(cv);
|
||||
return NULL;
|
||||
}
|
||||
@ -233,9 +212,7 @@ cv_create(const char *name)
|
||||
return cv;
|
||||
}
|
||||
|
||||
void
|
||||
cv_destroy(struct cv *cv)
|
||||
{
|
||||
void cv_destroy(struct cv *cv) {
|
||||
KASSERT(cv != NULL);
|
||||
|
||||
// add stuff here as needed
|
||||
@ -244,25 +221,19 @@ cv_destroy(struct cv *cv)
|
||||
kfree(cv);
|
||||
}
|
||||
|
||||
void
|
||||
cv_wait(struct cv *cv, struct lock *lock)
|
||||
{
|
||||
void cv_wait(struct cv *cv, struct lock *lock) {
|
||||
// Write this
|
||||
(void)cv; // suppress warning until code gets written
|
||||
(void)lock; // suppress warning until code gets written
|
||||
}
|
||||
|
||||
void
|
||||
cv_signal(struct cv *cv, struct lock *lock)
|
||||
{
|
||||
void cv_signal(struct cv *cv, struct lock *lock) {
|
||||
// Write this
|
||||
(void)cv; // suppress warning until code gets written
|
||||
(void)lock; // suppress warning until code gets written
|
||||
}
|
||||
|
||||
void
|
||||
cv_broadcast(struct cv *cv, struct lock *lock)
|
||||
{
|
||||
void cv_broadcast(struct cv *cv, struct lock *lock) {
|
||||
// Write this
|
||||
(void)cv; // suppress warning until code gets written
|
||||
(void)lock; // suppress warning until code gets written
|
||||
|
@ -51,7 +51,6 @@
|
||||
#include <mainbus.h>
|
||||
#include <vnode.h>
|
||||
|
||||
|
||||
/* Magic number used as a guard value on kernel thread stacks. */
|
||||
#define THREAD_STACK_MAGIC 0xbaadf00d
|
||||
|
||||
@ -76,10 +75,7 @@ static struct semaphore *cpu_startup_sem;
|
||||
* (sometimes) catch kernel stack overflows. Use thread_checkstack()
|
||||
* to test this.
|
||||
*/
|
||||
static
|
||||
void
|
||||
thread_checkstack_init(struct thread *thread)
|
||||
{
|
||||
static void thread_checkstack_init(struct thread *thread) {
|
||||
((uint32_t *)thread->t_stack)[0] = THREAD_STACK_MAGIC;
|
||||
((uint32_t *)thread->t_stack)[1] = THREAD_STACK_MAGIC;
|
||||
((uint32_t *)thread->t_stack)[2] = THREAD_STACK_MAGIC;
|
||||
@ -96,15 +92,12 @@ thread_checkstack_init(struct thread *thread)
|
||||
* cannot be freed (which in turn is the case if the stack is the boot
|
||||
* stack, and the thread is the boot thread) this doesn't do anything.
|
||||
*/
|
||||
static
|
||||
void
|
||||
thread_checkstack(struct thread *thread)
|
||||
{
|
||||
static void thread_checkstack(struct thread *thread) {
|
||||
if (thread->t_stack != NULL) {
|
||||
KASSERT(((uint32_t*)thread->t_stack)[0] == THREAD_STACK_MAGIC);
|
||||
KASSERT(((uint32_t*)thread->t_stack)[1] == THREAD_STACK_MAGIC);
|
||||
KASSERT(((uint32_t*)thread->t_stack)[2] == THREAD_STACK_MAGIC);
|
||||
KASSERT(((uint32_t*)thread->t_stack)[3] == THREAD_STACK_MAGIC);
|
||||
KASSERT(((uint32_t *)thread->t_stack)[0] == THREAD_STACK_MAGIC);
|
||||
KASSERT(((uint32_t *)thread->t_stack)[1] == THREAD_STACK_MAGIC);
|
||||
KASSERT(((uint32_t *)thread->t_stack)[2] == THREAD_STACK_MAGIC);
|
||||
KASSERT(((uint32_t *)thread->t_stack)[3] == THREAD_STACK_MAGIC);
|
||||
}
|
||||
}
|
||||
|
||||
@ -112,10 +105,7 @@ thread_checkstack(struct thread *thread)
|
||||
* Create a thread. This is used both to create a first thread
|
||||
* for each CPU and to create subsequent forked threads.
|
||||
*/
|
||||
static
|
||||
struct thread *
|
||||
thread_create(const char *name)
|
||||
{
|
||||
static struct thread *thread_create(const char *name) {
|
||||
struct thread *thread;
|
||||
|
||||
DEBUGASSERT(name != NULL);
|
||||
@ -160,9 +150,7 @@ thread_create(const char *name)
|
||||
* board config or whatnot) is tracked separately because it is not
|
||||
* necessarily anything sane or meaningful.
|
||||
*/
|
||||
struct cpu *
|
||||
cpu_create(unsigned hardware_number)
|
||||
{
|
||||
struct cpu *cpu_create(unsigned hardware_number) {
|
||||
struct cpu *c;
|
||||
int result;
|
||||
char namebuf[16];
|
||||
@ -208,8 +196,7 @@ cpu_create(unsigned hardware_number)
|
||||
* make it possible to free the boot stack?)
|
||||
*/
|
||||
/*c->c_curthread->t_stack = ... */
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
c->c_curthread->t_stack = kmalloc(STACK_SIZE);
|
||||
if (c->c_curthread->t_stack == NULL) {
|
||||
panic("cpu_create: couldn't allocate stack");
|
||||
@ -260,10 +247,7 @@ cpu_create(unsigned hardware_number)
|
||||
*
|
||||
* (Freeing the stack you're actually using to run is ... inadvisable.)
|
||||
*/
|
||||
static
|
||||
void
|
||||
thread_destroy(struct thread *thread)
|
||||
{
|
||||
static void thread_destroy(struct thread *thread) {
|
||||
KASSERT(thread != curthread);
|
||||
KASSERT(thread->t_state != S_RUN);
|
||||
|
||||
@ -293,10 +277,7 @@ thread_destroy(struct thread *thread)
|
||||
*
|
||||
* The list of zombies is per-cpu.
|
||||
*/
|
||||
static
|
||||
void
|
||||
exorcise(void)
|
||||
{
|
||||
static void exorcise(void) {
|
||||
struct thread *z;
|
||||
|
||||
while ((z = threadlist_remhead(&curcpu->c_zombies)) != NULL) {
|
||||
@ -311,9 +292,7 @@ exorcise(void)
|
||||
* possible) to make sure we don't end up letting any other threads
|
||||
* run.
|
||||
*/
|
||||
void
|
||||
thread_panic(void)
|
||||
{
|
||||
void thread_panic(void) {
|
||||
/*
|
||||
* Kill off other CPUs.
|
||||
*
|
||||
@ -353,9 +332,7 @@ thread_panic(void)
|
||||
/*
|
||||
* At system shutdown, ask the other CPUs to switch off.
|
||||
*/
|
||||
void
|
||||
thread_shutdown(void)
|
||||
{
|
||||
void thread_shutdown(void) {
|
||||
/*
|
||||
* Stop the other CPUs.
|
||||
*
|
||||
@ -368,9 +345,7 @@ thread_shutdown(void)
|
||||
/*
|
||||
* Thread system initialization.
|
||||
*/
|
||||
void
|
||||
thread_bootstrap(void)
|
||||
{
|
||||
void thread_bootstrap(void) {
|
||||
cpuarray_init(&allcpus);
|
||||
|
||||
/*
|
||||
@ -402,9 +377,7 @@ thread_bootstrap(void)
|
||||
* to do anything. The startup thread can just exit; we only need it
|
||||
* to be able to get into thread_switch() properly.
|
||||
*/
|
||||
void
|
||||
cpu_hatch(unsigned software_number)
|
||||
{
|
||||
void cpu_hatch(unsigned software_number) {
|
||||
char buf[64];
|
||||
|
||||
KASSERT(curcpu != NULL);
|
||||
@ -423,9 +396,7 @@ cpu_hatch(unsigned software_number)
|
||||
/*
|
||||
* Start up secondary cpus. Called from boot().
|
||||
*/
|
||||
void
|
||||
thread_start_cpus(void)
|
||||
{
|
||||
void thread_start_cpus(void) {
|
||||
char buf[64];
|
||||
unsigned i;
|
||||
|
||||
@ -435,7 +406,7 @@ thread_start_cpus(void)
|
||||
cpu_startup_sem = sem_create("cpu_hatch", 0);
|
||||
mainbus_start_cpus();
|
||||
|
||||
for (i=0; i<cpuarray_num(&allcpus) - 1; i++) {
|
||||
for (i = 0; i < cpuarray_num(&allcpus) - 1; i++) {
|
||||
P(cpu_startup_sem);
|
||||
}
|
||||
sem_destroy(cpu_startup_sem);
|
||||
@ -447,10 +418,8 @@ thread_start_cpus(void)
|
||||
*
|
||||
* targetcpu might be curcpu; it might not be, too.
|
||||
*/
|
||||
static
|
||||
void
|
||||
thread_make_runnable(struct thread *target, bool already_have_lock)
|
||||
{
|
||||
static void thread_make_runnable(struct thread *target,
|
||||
bool already_have_lock) {
|
||||
struct cpu *targetcpu;
|
||||
|
||||
/* Lock the run queue of the target thread's cpu. */
|
||||
@ -459,8 +428,7 @@ thread_make_runnable(struct thread *target, bool already_have_lock)
|
||||
if (already_have_lock) {
|
||||
/* The target thread's cpu should be already locked. */
|
||||
KASSERT(spinlock_do_i_hold(&targetcpu->c_runqueue_lock));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
spinlock_acquire(&targetcpu->c_runqueue_lock);
|
||||
}
|
||||
|
||||
@ -491,12 +459,9 @@ thread_make_runnable(struct thread *target, bool already_have_lock)
|
||||
* process is inherited from the caller. It will start on the same CPU
|
||||
* as the caller, unless the scheduler intervenes first.
|
||||
*/
|
||||
int
|
||||
thread_fork(const char *name,
|
||||
struct proc *proc,
|
||||
int thread_fork(const char *name, struct proc *proc,
|
||||
void (*entrypoint)(void *data1, unsigned long data2),
|
||||
void *data1, unsigned long data2)
|
||||
{
|
||||
void *data1, unsigned long data2) {
|
||||
struct thread *newthread;
|
||||
int result;
|
||||
|
||||
@ -557,10 +522,8 @@ thread_fork(const char *name,
|
||||
* WC, protected by the spinlock LK. Otherwise WC and Lk should be
|
||||
* NULL.
|
||||
*/
|
||||
static
|
||||
void
|
||||
thread_switch(threadstate_t newstate, struct wchan *wc, struct spinlock *lk)
|
||||
{
|
||||
static void thread_switch(threadstate_t newstate, struct wchan *wc,
|
||||
struct spinlock *lk) {
|
||||
struct thread *cur, *next;
|
||||
int spl;
|
||||
|
||||
@ -709,7 +672,6 @@ thread_switch(threadstate_t newstate, struct wchan *wc, struct spinlock *lk)
|
||||
* thread_startup.
|
||||
*/
|
||||
|
||||
|
||||
/* Clear the wait channel and set the thread state. */
|
||||
cur->t_wchan_name = NULL;
|
||||
cur->t_state = S_RUN;
|
||||
@ -735,10 +697,8 @@ thread_switch(threadstate_t newstate, struct wchan *wc, struct spinlock *lk)
|
||||
* thread_switch, the beginning part of this function must match the
|
||||
* tail of thread_switch.
|
||||
*/
|
||||
void
|
||||
thread_startup(void (*entrypoint)(void *data1, unsigned long data2),
|
||||
void *data1, unsigned long data2)
|
||||
{
|
||||
void thread_startup(void (*entrypoint)(void *data1, unsigned long data2),
|
||||
void *data1, unsigned long data2) {
|
||||
struct thread *cur;
|
||||
|
||||
cur = curthread;
|
||||
@ -775,9 +735,7 @@ thread_startup(void (*entrypoint)(void *data1, unsigned long data2),
|
||||
*
|
||||
* Does not return.
|
||||
*/
|
||||
void
|
||||
thread_exit(void)
|
||||
{
|
||||
void thread_exit(void) {
|
||||
struct thread *cur;
|
||||
|
||||
cur = curthread;
|
||||
@ -803,11 +761,7 @@ thread_exit(void)
|
||||
/*
|
||||
* Yield the cpu to another process, but stay runnable.
|
||||
*/
|
||||
void
|
||||
thread_yield(void)
|
||||
{
|
||||
thread_switch(S_READY, NULL, NULL);
|
||||
}
|
||||
void thread_yield(void) { thread_switch(S_READY, NULL, NULL); }
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
@ -818,9 +772,7 @@ thread_yield(void)
|
||||
* the current CPU's run queue by job priority.
|
||||
*/
|
||||
|
||||
void
|
||||
schedule(void)
|
||||
{
|
||||
void schedule(void) {
|
||||
/*
|
||||
* You can write this. If we do nothing, threads will run in
|
||||
* round-robin fashion.
|
||||
@ -844,9 +796,7 @@ schedule(void)
|
||||
* System/161 does not (yet) model such cache effects, we'll be very
|
||||
* aggressive.
|
||||
*/
|
||||
void
|
||||
thread_consider_migration(void)
|
||||
{
|
||||
void thread_consider_migration(void) {
|
||||
unsigned my_count, total_count, one_share, to_send;
|
||||
unsigned i, numcpus;
|
||||
struct cpu *c;
|
||||
@ -855,7 +805,7 @@ thread_consider_migration(void)
|
||||
|
||||
my_count = total_count = 0;
|
||||
numcpus = cpuarray_num(&allcpus);
|
||||
for (i=0; i<numcpus; i++) {
|
||||
for (i = 0; i < numcpus; i++) {
|
||||
c = cpuarray_get(&allcpus, i);
|
||||
spinlock_acquire(&c->c_runqueue_lock);
|
||||
total_count += c->c_runqueue.tl_count;
|
||||
@ -873,13 +823,13 @@ thread_consider_migration(void)
|
||||
to_send = my_count - one_share;
|
||||
threadlist_init(&victims);
|
||||
spinlock_acquire(&curcpu->c_runqueue_lock);
|
||||
for (i=0; i<to_send; i++) {
|
||||
for (i = 0; i < to_send; i++) {
|
||||
t = threadlist_remtail(&curcpu->c_runqueue);
|
||||
threadlist_addhead(&victims, t);
|
||||
}
|
||||
spinlock_release(&curcpu->c_runqueue_lock);
|
||||
|
||||
for (i=0; i < numcpus && to_send > 0; i++) {
|
||||
for (i = 0; i < numcpus && to_send > 0; i++) {
|
||||
c = cpuarray_get(&allcpus, i);
|
||||
if (c == curcpu->c_self) {
|
||||
continue;
|
||||
@ -917,9 +867,8 @@ thread_consider_migration(void)
|
||||
|
||||
t->t_cpu = c;
|
||||
threadlist_addtail(&c->c_runqueue, t);
|
||||
DEBUG(DB_THREADS,
|
||||
"Migrated thread %s: cpu %u -> %u",
|
||||
t->t_name, curcpu->c_number, c->c_number);
|
||||
DEBUG(DB_THREADS, "Migrated thread %s: cpu %u -> %u", t->t_name,
|
||||
curcpu->c_number, c->c_number);
|
||||
to_send--;
|
||||
if (c->c_isidle) {
|
||||
/*
|
||||
@ -963,9 +912,7 @@ thread_consider_migration(void)
|
||||
* arrangements should be made to free it after the wait channel is
|
||||
* destroyed.
|
||||
*/
|
||||
struct wchan *
|
||||
wchan_create(const char *name)
|
||||
{
|
||||
struct wchan *wchan_create(const char *name) {
|
||||
struct wchan *wc;
|
||||
|
||||
wc = kmalloc(sizeof(*wc));
|
||||
@ -982,9 +929,7 @@ wchan_create(const char *name)
|
||||
* Destroy a wait channel. Must be empty and unlocked.
|
||||
* (The corresponding cleanup functions require this.)
|
||||
*/
|
||||
void
|
||||
wchan_destroy(struct wchan *wc)
|
||||
{
|
||||
void wchan_destroy(struct wchan *wc) {
|
||||
threadlist_cleanup(&wc->wc_threads);
|
||||
kfree(wc);
|
||||
}
|
||||
@ -996,9 +941,7 @@ wchan_destroy(struct wchan *wc)
|
||||
* be locked. The call to thread_switch unlocks it; we relock it
|
||||
* before returning.
|
||||
*/
|
||||
void
|
||||
wchan_sleep(struct wchan *wc, struct spinlock *lk)
|
||||
{
|
||||
void wchan_sleep(struct wchan *wc, struct spinlock *lk) {
|
||||
/* may not sleep in an interrupt handler */
|
||||
KASSERT(!curthread->t_in_interrupt);
|
||||
|
||||
@ -1015,9 +958,7 @@ wchan_sleep(struct wchan *wc, struct spinlock *lk)
|
||||
/*
|
||||
* Wake up one thread sleeping on a wait channel.
|
||||
*/
|
||||
void
|
||||
wchan_wakeone(struct wchan *wc, struct spinlock *lk)
|
||||
{
|
||||
void wchan_wakeone(struct wchan *wc, struct spinlock *lk) {
|
||||
struct thread *target;
|
||||
|
||||
KASSERT(spinlock_do_i_hold(lk));
|
||||
@ -1044,9 +985,7 @@ wchan_wakeone(struct wchan *wc, struct spinlock *lk)
|
||||
/*
|
||||
* Wake up all threads sleeping on a wait channel.
|
||||
*/
|
||||
void
|
||||
wchan_wakeall(struct wchan *wc, struct spinlock *lk)
|
||||
{
|
||||
void wchan_wakeall(struct wchan *wc, struct spinlock *lk) {
|
||||
struct thread *target;
|
||||
struct threadlist list;
|
||||
|
||||
@ -1078,9 +1017,7 @@ wchan_wakeall(struct wchan *wc, struct spinlock *lk)
|
||||
* Return nonzero if there are no threads sleeping on the channel.
|
||||
* This is meant to be used only for diagnostic purposes.
|
||||
*/
|
||||
bool
|
||||
wchan_isempty(struct wchan *wc, struct spinlock *lk)
|
||||
{
|
||||
bool wchan_isempty(struct wchan *wc, struct spinlock *lk) {
|
||||
bool ret;
|
||||
|
||||
KASSERT(spinlock_do_i_hold(lk));
|
||||
@ -1098,9 +1035,7 @@ wchan_isempty(struct wchan *wc, struct spinlock *lk)
|
||||
/*
|
||||
* Send an IPI (inter-processor interrupt) to the specified CPU.
|
||||
*/
|
||||
void
|
||||
ipi_send(struct cpu *target, int code)
|
||||
{
|
||||
void ipi_send(struct cpu *target, int code) {
|
||||
KASSERT(code >= 0 && code < 32);
|
||||
|
||||
spinlock_acquire(&target->c_ipi_lock);
|
||||
@ -1112,13 +1047,11 @@ ipi_send(struct cpu *target, int code)
|
||||
/*
|
||||
* Send an IPI to all CPUs.
|
||||
*/
|
||||
void
|
||||
ipi_broadcast(int code)
|
||||
{
|
||||
void ipi_broadcast(int code) {
|
||||
unsigned i;
|
||||
struct cpu *c;
|
||||
|
||||
for (i=0; i < cpuarray_num(&allcpus); i++) {
|
||||
for (i = 0; i < cpuarray_num(&allcpus); i++) {
|
||||
c = cpuarray_get(&allcpus, i);
|
||||
if (c != curcpu->c_self) {
|
||||
ipi_send(c, code);
|
||||
@ -1129,9 +1062,7 @@ ipi_broadcast(int code)
|
||||
/*
|
||||
* Send a TLB shootdown IPI to the specified CPU.
|
||||
*/
|
||||
void
|
||||
ipi_tlbshootdown(struct cpu *target, const struct tlbshootdown *mapping)
|
||||
{
|
||||
void ipi_tlbshootdown(struct cpu *target, const struct tlbshootdown *mapping) {
|
||||
unsigned n;
|
||||
|
||||
spinlock_acquire(&target->c_ipi_lock);
|
||||
@ -1148,10 +1079,9 @@ ipi_tlbshootdown(struct cpu *target, const struct tlbshootdown *mapping)
|
||||
* reduce the number of unnecessary shootdowns.
|
||||
*/
|
||||
panic("ipi_tlbshootdown: Too many shootdowns queued\n");
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
target->c_shootdown[n] = *mapping;
|
||||
target->c_numshootdown = n+1;
|
||||
target->c_numshootdown = n + 1;
|
||||
}
|
||||
|
||||
target->c_ipi_pending |= (uint32_t)1 << IPI_TLBSHOOTDOWN;
|
||||
@ -1163,9 +1093,7 @@ ipi_tlbshootdown(struct cpu *target, const struct tlbshootdown *mapping)
|
||||
/*
|
||||
* Handle an incoming interprocessor interrupt.
|
||||
*/
|
||||
void
|
||||
interprocessor_interrupt(void)
|
||||
{
|
||||
void interprocessor_interrupt(void) {
|
||||
uint32_t bits;
|
||||
unsigned i;
|
||||
|
||||
@ -1182,8 +1110,7 @@ interprocessor_interrupt(void)
|
||||
spinlock_release(&curcpu->c_ipi_lock);
|
||||
spinlock_acquire(&curcpu->c_runqueue_lock);
|
||||
if (!curcpu->c_isidle) {
|
||||
kprintf("cpu%d: offline: warning: not idle\n",
|
||||
curcpu->c_number);
|
||||
kprintf("cpu%d: offline: warning: not idle\n", curcpu->c_number);
|
||||
}
|
||||
spinlock_release(&curcpu->c_runqueue_lock);
|
||||
kprintf("cpu%d: offline.\n", curcpu->c_number);
|
||||
@ -1201,7 +1128,7 @@ interprocessor_interrupt(void)
|
||||
* need to release the ipi lock while calling
|
||||
* vm_tlbshootdown.
|
||||
*/
|
||||
for (i=0; i<curcpu->c_numshootdown; i++) {
|
||||
for (i = 0; i < curcpu->c_numshootdown; i++) {
|
||||
vm_tlbshootdown(&curcpu->c_shootdown[i]);
|
||||
}
|
||||
curcpu->c_numshootdown = 0;
|
||||
|
@ -36,18 +36,14 @@
|
||||
* Kernel malloc.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Fill a block with 0xdeadbeef.
|
||||
*/
|
||||
static
|
||||
void
|
||||
fill_deadbeef(void *vptr, size_t len)
|
||||
{
|
||||
static void fill_deadbeef(void *vptr, size_t len) {
|
||||
uint32_t *ptr = vptr;
|
||||
size_t i;
|
||||
|
||||
for (i=0; i<len/sizeof(uint32_t); i++) {
|
||||
for (i = 0; i < len / sizeof(uint32_t); i++) {
|
||||
ptr[i] = 0xdeadbeef;
|
||||
}
|
||||
}
|
||||
@ -126,7 +122,7 @@ fill_deadbeef(void *vptr, size_t len)
|
||||
#if PAGE_SIZE == 4096
|
||||
|
||||
#define NSIZES 8
|
||||
static const size_t sizes[NSIZES] = { 16, 32, 64, 128, 256, 512, 1024, 2048 };
|
||||
static const size_t sizes[NSIZES] = {16, 32, 64, 128, 256, 512, 1024, 2048};
|
||||
|
||||
#define SMALLEST_SUBPAGE_SIZE 16
|
||||
#define LARGEST_SUBPAGE_SIZE 2048
|
||||
@ -155,7 +151,7 @@ struct pageref {
|
||||
|
||||
#define PR_PAGEADDR(pr) ((pr)->pageaddr_and_blocktype & PAGE_FRAME)
|
||||
#define PR_BLOCKTYPE(pr) ((pr)->pageaddr_and_blocktype & ~PAGE_FRAME)
|
||||
#define MKPAB(pa, blk) (((pa)&PAGE_FRAME) | ((blk) & ~PAGE_FRAME))
|
||||
#define MKPAB(pa, blk) (((pa) & PAGE_FRAME) | ((blk) & ~PAGE_FRAME))
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
@ -215,10 +211,7 @@ static struct kheap_root kheaproots[NUM_PAGEREFPAGES];
|
||||
/*
|
||||
* Allocate a page to hold pagerefs.
|
||||
*/
|
||||
static
|
||||
void
|
||||
allocpagerefpage(struct kheap_root *root)
|
||||
{
|
||||
static void allocpagerefpage(struct kheap_root *root) {
|
||||
vaddr_t va;
|
||||
|
||||
KASSERT(root->page == NULL);
|
||||
@ -253,16 +246,13 @@ allocpagerefpage(struct kheap_root *root)
|
||||
/*
|
||||
* Allocate a pageref structure.
|
||||
*/
|
||||
static
|
||||
struct pageref *
|
||||
allocpageref(void)
|
||||
{
|
||||
unsigned i,j;
|
||||
static struct pageref *allocpageref(void) {
|
||||
unsigned i, j;
|
||||
uint32_t k;
|
||||
unsigned whichroot;
|
||||
struct kheap_root *root;
|
||||
|
||||
for (whichroot=0; whichroot < NUM_PAGEREFPAGES; whichroot++) {
|
||||
for (whichroot = 0; whichroot < NUM_PAGEREFPAGES; whichroot++) {
|
||||
root = &kheaproots[whichroot];
|
||||
if (root->numinuse >= NPAGEREFS_PER_PAGE) {
|
||||
continue;
|
||||
@ -271,13 +261,13 @@ allocpageref(void)
|
||||
/*
|
||||
* This should probably not be a linear search.
|
||||
*/
|
||||
for (i=0; i<INUSE_WORDS; i++) {
|
||||
if (root->pagerefs_inuse[i]==0xffffffff) {
|
||||
for (i = 0; i < INUSE_WORDS; i++) {
|
||||
if (root->pagerefs_inuse[i] == 0xffffffff) {
|
||||
/* full */
|
||||
continue;
|
||||
}
|
||||
for (k=1,j=0; k!=0; k<<=1,j++) {
|
||||
if ((root->pagerefs_inuse[i] & k)==0) {
|
||||
for (k = 1, j = 0; k != 0; k <<= 1, j++) {
|
||||
if ((root->pagerefs_inuse[i] & k) == 0) {
|
||||
root->pagerefs_inuse[i] |= k;
|
||||
root->numinuse++;
|
||||
if (root->page == NULL) {
|
||||
@ -286,7 +276,7 @@ allocpageref(void)
|
||||
if (root->page == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return &root->page->refs[i*32 + j];
|
||||
return &root->page->refs[i * 32 + j];
|
||||
}
|
||||
}
|
||||
KASSERT(0);
|
||||
@ -300,17 +290,14 @@ allocpageref(void)
|
||||
/*
|
||||
* Release a pageref structure.
|
||||
*/
|
||||
static
|
||||
void
|
||||
freepageref(struct pageref *p)
|
||||
{
|
||||
static void freepageref(struct pageref *p) {
|
||||
size_t i, j;
|
||||
uint32_t k;
|
||||
unsigned whichroot;
|
||||
struct kheap_root *root;
|
||||
struct pagerefpage *page;
|
||||
|
||||
for (whichroot=0; whichroot < NUM_PAGEREFPAGES; whichroot++) {
|
||||
for (whichroot = 0; whichroot < NUM_PAGEREFPAGES; whichroot++) {
|
||||
root = &kheaproots[whichroot];
|
||||
|
||||
page = root->page;
|
||||
@ -319,12 +306,12 @@ freepageref(struct pageref *p)
|
||||
continue;
|
||||
}
|
||||
|
||||
j = p-page->refs;
|
||||
j = p - page->refs;
|
||||
/* note: j is unsigned, don't test < 0 */
|
||||
if (j < NPAGEREFS_PER_PAGE) {
|
||||
/* on this page */
|
||||
i = j/32;
|
||||
k = ((uint32_t)1) << (j%32);
|
||||
i = j / 32;
|
||||
k = ((uint32_t)1) << (j % 32);
|
||||
KASSERT((root->pagerefs_inuse[i] & k) != 0);
|
||||
root->pagerefs_inuse[i] &= ~k;
|
||||
KASSERT(root->numinuse > 0);
|
||||
@ -365,10 +352,8 @@ static struct pageref *allbase;
|
||||
/*
|
||||
* Set up the guard values in a block we're about to return.
|
||||
*/
|
||||
static
|
||||
void *
|
||||
establishguardband(void *block, size_t clientsize, size_t blocksize)
|
||||
{
|
||||
static void *establishguardband(void *block, size_t clientsize,
|
||||
size_t blocksize) {
|
||||
vaddr_t lowguard, lowsize, data, enddata, highguard, highsize, i;
|
||||
|
||||
KASSERT(clientsize + GUARD_OVERHEAD <= blocksize);
|
||||
@ -383,10 +368,10 @@ establishguardband(void *block, size_t clientsize, size_t blocksize)
|
||||
|
||||
*(uint16_t *)lowguard = GUARD_HALFWORD;
|
||||
*(uint16_t *)lowsize = clientsize;
|
||||
for (i=data; i<enddata; i++) {
|
||||
for (i = data; i < enddata; i++) {
|
||||
*(uint8_t *)i = GUARD_RETBYTE;
|
||||
}
|
||||
for (i=enddata; i<highguard; i++) {
|
||||
for (i = enddata; i < highguard; i++) {
|
||||
*(uint8_t *)i = GUARD_FILLBYTE;
|
||||
}
|
||||
*(uint16_t *)highguard = GUARD_HALFWORD;
|
||||
@ -398,10 +383,8 @@ establishguardband(void *block, size_t clientsize, size_t blocksize)
|
||||
/*
|
||||
* Validate the guard values in an existing block.
|
||||
*/
|
||||
static
|
||||
void
|
||||
checkguardband(vaddr_t blockaddr, size_t smallerblocksize, size_t blocksize)
|
||||
{
|
||||
static void checkguardband(vaddr_t blockaddr, size_t smallerblocksize,
|
||||
size_t blocksize) {
|
||||
/*
|
||||
* The first two bytes of the block are the lower guard band.
|
||||
* The next two bytes are the real size (the size of the
|
||||
@ -430,7 +413,7 @@ checkguardband(vaddr_t blockaddr, size_t smallerblocksize, size_t blocksize)
|
||||
KASSERT(clientsize + GUARD_OVERHEAD > smallerblocksize);
|
||||
KASSERT(clientsize + GUARD_OVERHEAD <= blocksize);
|
||||
enddata = data + clientsize;
|
||||
for (i=enddata; i<highguard; i++) {
|
||||
for (i = enddata; i < highguard; i++) {
|
||||
KASSERT(*(uint8_t *)i == GUARD_FILLBYTE);
|
||||
}
|
||||
}
|
||||
@ -457,14 +440,11 @@ checkguardband(vaddr_t blockaddr, size_t smallerblocksize, size_t blocksize)
|
||||
* The first word of the block is a freelist pointer and should not be
|
||||
* deadbeef; the rest of the block should be only deadbeef.
|
||||
*/
|
||||
static
|
||||
void
|
||||
checkdeadbeef(void *block, size_t blocksize)
|
||||
{
|
||||
static void checkdeadbeef(void *block, size_t blocksize) {
|
||||
uint32_t *ptr = block;
|
||||
size_t i;
|
||||
|
||||
for (i=1; i < blocksize/sizeof(uint32_t); i++) {
|
||||
for (i = 1; i < blocksize / sizeof(uint32_t); i++) {
|
||||
KASSERT(ptr[i] == 0xdeadbeef);
|
||||
}
|
||||
}
|
||||
@ -490,14 +470,11 @@ checkdeadbeef(void *block, size_t blocksize)
|
||||
* assertion as a bit in isfree is set twice; if not, a circular
|
||||
* freelist will cause an infinite loop.
|
||||
*/
|
||||
static
|
||||
void
|
||||
checksubpage(struct pageref *pr)
|
||||
{
|
||||
static void checksubpage(struct pageref *pr) {
|
||||
vaddr_t prpage, fla;
|
||||
struct freelist *fl;
|
||||
int blktype;
|
||||
int nfree=0;
|
||||
int nfree = 0;
|
||||
size_t blocksize;
|
||||
#ifdef CHECKGUARDS
|
||||
const unsigned maxblocks = PAGE_SIZE / SMALLEST_SUBPAGE_SIZE;
|
||||
@ -510,7 +487,7 @@ checksubpage(struct pageref *pr)
|
||||
KASSERT(spinlock_do_i_hold(&kmalloc_spinlock));
|
||||
|
||||
if (pr->freelist_offset == INVALID_OFFSET) {
|
||||
KASSERT(pr->nfree==0);
|
||||
KASSERT(pr->nfree == 0);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -521,7 +498,7 @@ checksubpage(struct pageref *pr)
|
||||
|
||||
#ifdef CHECKGUARDS
|
||||
smallerblocksize = blktype > 0 ? sizes[blktype - 1] : 0;
|
||||
for (i=0; i<numfreewords; i++) {
|
||||
for (i = 0; i < numfreewords; i++) {
|
||||
isfree[i] = 0;
|
||||
}
|
||||
#endif
|
||||
@ -540,12 +517,12 @@ checksubpage(struct pageref *pr)
|
||||
for (; fl != NULL; fl = fl->next) {
|
||||
fla = (vaddr_t)fl;
|
||||
KASSERT(fla >= prpage && fla < prpage + PAGE_SIZE);
|
||||
KASSERT((fla-prpage) % blocksize == 0);
|
||||
KASSERT((fla - prpage) % blocksize == 0);
|
||||
#ifdef CHECKBEEF
|
||||
checkdeadbeef(fl, blocksize);
|
||||
#endif
|
||||
#ifdef CHECKGUARDS
|
||||
blocknum = (fla-prpage) / blocksize;
|
||||
blocknum = (fla - prpage) / blocksize;
|
||||
mask = 1U << (blocknum % 32);
|
||||
KASSERT((isfree[blocknum / 32] & mask) == 0);
|
||||
isfree[blocknum / 32] |= mask;
|
||||
@ -553,15 +530,14 @@ checksubpage(struct pageref *pr)
|
||||
KASSERT(fl->next != fl);
|
||||
nfree++;
|
||||
}
|
||||
KASSERT(nfree==pr->nfree);
|
||||
KASSERT(nfree == pr->nfree);
|
||||
|
||||
#ifdef CHECKGUARDS
|
||||
numblocks = PAGE_SIZE / blocksize;
|
||||
for (i=0; i<numblocks; i++) {
|
||||
for (i = 0; i < numblocks; i++) {
|
||||
mask = 1U << (i % 32);
|
||||
if ((isfree[i / 32] & mask) == 0) {
|
||||
checkguardband(prpage + i * blocksize,
|
||||
smallerblocksize, blocksize);
|
||||
checkguardband(prpage + i * blocksize, smallerblocksize, blocksize);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -575,17 +551,14 @@ checksubpage(struct pageref *pr)
|
||||
* Run checksubpage on all heap pages. This also checks that the
|
||||
* linked lists of pagerefs are more or less intact.
|
||||
*/
|
||||
static
|
||||
void
|
||||
checksubpages(void)
|
||||
{
|
||||
static void checksubpages(void) {
|
||||
struct pageref *pr;
|
||||
int i;
|
||||
unsigned sc=0, ac=0;
|
||||
unsigned sc = 0, ac = 0;
|
||||
|
||||
KASSERT(spinlock_do_i_hold(&kmalloc_spinlock));
|
||||
|
||||
for (i=0; i<NSIZES; i++) {
|
||||
for (i = 0; i < NSIZES; i++) {
|
||||
for (pr = sizebases[i]; pr != NULL; pr = pr->next_samesize) {
|
||||
checksubpage(pr);
|
||||
KASSERT(sc < TOTAL_PAGEREFS);
|
||||
@ -599,7 +572,7 @@ checksubpages(void)
|
||||
ac++;
|
||||
}
|
||||
|
||||
KASSERT(sc==ac);
|
||||
KASSERT(sc == ac);
|
||||
}
|
||||
#else
|
||||
#define checksubpages()
|
||||
@ -622,10 +595,7 @@ static unsigned mallocgeneration;
|
||||
/*
|
||||
* Label a block of memory.
|
||||
*/
|
||||
static
|
||||
void *
|
||||
establishlabel(void *block, vaddr_t label)
|
||||
{
|
||||
static void *establishlabel(void *block, vaddr_t label) {
|
||||
struct malloclabel *ml;
|
||||
|
||||
ml = block;
|
||||
@ -635,10 +605,7 @@ establishlabel(void *block, vaddr_t label)
|
||||
return ml;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
dump_subpage(struct pageref *pr, unsigned generation)
|
||||
{
|
||||
static void dump_subpage(struct pageref *pr, unsigned generation) {
|
||||
unsigned blocksize = sizes[PR_BLOCKTYPE(pr)];
|
||||
unsigned numblocks = PAGE_SIZE / blocksize;
|
||||
unsigned numfreewords = DIVROUNDUP(numblocks, 32);
|
||||
@ -649,7 +616,7 @@ dump_subpage(struct pageref *pr, unsigned generation)
|
||||
struct malloclabel *ml;
|
||||
unsigned i;
|
||||
|
||||
for (i=0; i<numfreewords; i++) {
|
||||
for (i = 0; i < numfreewords; i++) {
|
||||
isfree[i] = 0;
|
||||
}
|
||||
|
||||
@ -661,7 +628,7 @@ dump_subpage(struct pageref *pr, unsigned generation)
|
||||
isfree[i / 32] |= mask;
|
||||
}
|
||||
|
||||
for (i=0; i<numblocks; i++) {
|
||||
for (i = 0; i < numblocks; i++) {
|
||||
mask = 1U << (i % 32);
|
||||
if (isfree[i / 32] & mask) {
|
||||
continue;
|
||||
@ -671,20 +638,17 @@ dump_subpage(struct pageref *pr, unsigned generation)
|
||||
if (ml->generation != generation) {
|
||||
continue;
|
||||
}
|
||||
kprintf("%5zu bytes at %p, allocated at %p\n",
|
||||
blocksize, (void *)blockaddr, (void *)ml->label);
|
||||
kprintf("%5zu bytes at %p, allocated at %p\n", blocksize, (void *)blockaddr,
|
||||
(void *)ml->label);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
dump_subpages(unsigned generation)
|
||||
{
|
||||
static void dump_subpages(unsigned generation) {
|
||||
struct pageref *pr;
|
||||
int i;
|
||||
|
||||
kprintf("Remaining allocations from generation %u:\n", generation);
|
||||
for (i=0; i<NSIZES; i++) {
|
||||
for (i = 0; i < NSIZES; i++) {
|
||||
for (pr = sizebases[i]; pr != NULL; pr = pr->next_samesize) {
|
||||
dump_subpage(pr, generation);
|
||||
}
|
||||
@ -697,9 +661,7 @@ dump_subpages(unsigned generation)
|
||||
|
||||
#endif /* LABELS */
|
||||
|
||||
void
|
||||
kheap_nextgeneration(void)
|
||||
{
|
||||
void kheap_nextgeneration(void) {
|
||||
#ifdef LABELS
|
||||
spinlock_acquire(&kmalloc_spinlock);
|
||||
mallocgeneration++;
|
||||
@ -707,9 +669,7 @@ kheap_nextgeneration(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
kheap_dump(void)
|
||||
{
|
||||
void kheap_dump(void) {
|
||||
#ifdef LABELS
|
||||
/* print the whole thing with interrupts off */
|
||||
spinlock_acquire(&kmalloc_spinlock);
|
||||
@ -720,15 +680,13 @@ kheap_dump(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
kheap_dumpall(void)
|
||||
{
|
||||
void kheap_dumpall(void) {
|
||||
#ifdef LABELS
|
||||
unsigned i;
|
||||
|
||||
/* print the whole thing with interrupts off */
|
||||
spinlock_acquire(&kmalloc_spinlock);
|
||||
for (i=0; i<=mallocgeneration; i++) {
|
||||
for (i = 0; i <= mallocgeneration; i++) {
|
||||
dump_subpages(i);
|
||||
}
|
||||
spinlock_release(&kmalloc_spinlock);
|
||||
@ -742,21 +700,18 @@ kheap_dumpall(void)
|
||||
/*
|
||||
* Print the allocated/freed map of a single kernel heap page.
|
||||
*/
|
||||
static
|
||||
void
|
||||
subpage_stats(struct pageref *pr)
|
||||
{
|
||||
static void subpage_stats(struct pageref *pr) {
|
||||
vaddr_t prpage, fla;
|
||||
struct freelist *fl;
|
||||
int blktype;
|
||||
unsigned i, n, index;
|
||||
uint32_t freemap[PAGE_SIZE / (SMALLEST_SUBPAGE_SIZE*32)];
|
||||
uint32_t freemap[PAGE_SIZE / (SMALLEST_SUBPAGE_SIZE * 32)];
|
||||
|
||||
checksubpage(pr);
|
||||
KASSERT(spinlock_do_i_hold(&kmalloc_spinlock));
|
||||
|
||||
/* clear freemap[] */
|
||||
for (i=0; i<ARRAYCOUNT(freemap); i++) {
|
||||
for (i = 0; i < ARRAYCOUNT(freemap); i++) {
|
||||
freemap[i] = 0;
|
||||
}
|
||||
|
||||
@ -774,20 +729,19 @@ subpage_stats(struct pageref *pr)
|
||||
|
||||
for (; fl != NULL; fl = fl->next) {
|
||||
fla = (vaddr_t)fl;
|
||||
index = (fla-prpage) / sizes[blktype];
|
||||
KASSERT(index<n);
|
||||
freemap[index/32] |= (1<<(index%32));
|
||||
index = (fla - prpage) / sizes[blktype];
|
||||
KASSERT(index < n);
|
||||
freemap[index / 32] |= (1 << (index % 32));
|
||||
}
|
||||
}
|
||||
|
||||
kprintf("at 0x%08lx: size %-4lu %u/%u free\n",
|
||||
(unsigned long)prpage, (unsigned long) sizes[blktype],
|
||||
(unsigned) pr->nfree, n);
|
||||
kprintf("at 0x%08lx: size %-4lu %u/%u free\n", (unsigned long)prpage,
|
||||
(unsigned long)sizes[blktype], (unsigned)pr->nfree, n);
|
||||
kprintf(" ");
|
||||
for (i=0; i<n; i++) {
|
||||
int val = (freemap[i/32] & (1<<(i%32)))!=0;
|
||||
for (i = 0; i < n; i++) {
|
||||
int val = (freemap[i / 32] & (1 << (i % 32))) != 0;
|
||||
kprintf("%c", val ? '.' : '*');
|
||||
if (i%64==63 && i<n-1) {
|
||||
if (i % 64 == 63 && i < n - 1) {
|
||||
kprintf("\n ");
|
||||
}
|
||||
}
|
||||
@ -797,9 +751,7 @@ subpage_stats(struct pageref *pr)
|
||||
/*
|
||||
* Print the whole heap.
|
||||
*/
|
||||
void
|
||||
kheap_printstats(void)
|
||||
{
|
||||
void kheap_printstats(void) {
|
||||
struct pageref *pr;
|
||||
|
||||
/* print the whole thing with interrupts off */
|
||||
@ -819,13 +771,10 @@ kheap_printstats(void)
|
||||
/*
|
||||
* Remove a pageref from both lists that it's on.
|
||||
*/
|
||||
static
|
||||
void
|
||||
remove_lists(struct pageref *pr, int blktype)
|
||||
{
|
||||
static void remove_lists(struct pageref *pr, int blktype) {
|
||||
struct pageref **guy;
|
||||
|
||||
KASSERT(blktype>=0 && blktype<NSIZES);
|
||||
KASSERT(blktype >= 0 && blktype < NSIZES);
|
||||
|
||||
for (guy = &sizebases[blktype]; *guy; guy = &(*guy)->next_samesize) {
|
||||
checksubpage(*guy);
|
||||
@ -848,19 +797,15 @@ remove_lists(struct pageref *pr, int blktype)
|
||||
* Given a requested client size, return the block type, that is, the
|
||||
* index into the sizes[] array for the block size to use.
|
||||
*/
|
||||
static
|
||||
inline
|
||||
int blocktype(size_t clientsz)
|
||||
{
|
||||
static inline int blocktype(size_t clientsz) {
|
||||
unsigned i;
|
||||
for (i=0; i<NSIZES; i++) {
|
||||
for (i = 0; i < NSIZES; i++) {
|
||||
if (clientsz <= sizes[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
panic("Subpage allocator cannot handle allocation of size %zu\n",
|
||||
clientsz);
|
||||
panic("Subpage allocator cannot handle allocation of size %zu\n", clientsz);
|
||||
|
||||
// keep compiler happy
|
||||
return 0;
|
||||
@ -870,14 +815,12 @@ int blocktype(size_t clientsz)
|
||||
* Allocate a block of size SZ, where SZ is not large enough to
|
||||
* warrant a whole-page allocation.
|
||||
*/
|
||||
static
|
||||
void *
|
||||
subpage_kmalloc(size_t sz
|
||||
static void *subpage_kmalloc(size_t sz
|
||||
#ifdef LABELS
|
||||
, vaddr_t label
|
||||
,
|
||||
vaddr_t label
|
||||
#endif
|
||||
)
|
||||
{
|
||||
) {
|
||||
unsigned blktype; // index into sizes[] that we're using
|
||||
struct pageref *pr; // pageref for page we're allocating from
|
||||
vaddr_t prpage; // PR_PAGEADDR(pr)
|
||||
@ -935,8 +878,7 @@ subpage_kmalloc(size_t sz
|
||||
fla = (vaddr_t)fl;
|
||||
KASSERT(fla - prpage < PAGE_SIZE);
|
||||
pr->freelist_offset = fla - prpage;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
KASSERT(pr->nfree == 0);
|
||||
pr->freelist_offset = INVALID_OFFSET;
|
||||
}
|
||||
@ -965,7 +907,7 @@ subpage_kmalloc(size_t sz
|
||||
|
||||
spinlock_release(&kmalloc_spinlock);
|
||||
prpage = alloc_kpages(1);
|
||||
if (prpage==0) {
|
||||
if (prpage == 0) {
|
||||
/* Out of memory. */
|
||||
kprintf("kmalloc: Subpage allocator couldn't get a page\n");
|
||||
return NULL;
|
||||
@ -978,7 +920,7 @@ subpage_kmalloc(size_t sz
|
||||
spinlock_acquire(&kmalloc_spinlock);
|
||||
|
||||
pr = allocpageref();
|
||||
if (pr==NULL) {
|
||||
if (pr == NULL) {
|
||||
/* Couldn't allocate accounting space for the new page. */
|
||||
spinlock_release(&kmalloc_spinlock);
|
||||
free_kpages(prpage);
|
||||
@ -998,14 +940,14 @@ subpage_kmalloc(size_t sz
|
||||
fla = prpage;
|
||||
fl = (struct freelist *)fla;
|
||||
fl->next = NULL;
|
||||
for (i=1; i<pr->nfree; i++) {
|
||||
fl = (struct freelist *)(fla + i*sizes[blktype]);
|
||||
fl->next = (struct freelist *)(fla + (i-1)*sizes[blktype]);
|
||||
for (i = 1; i < pr->nfree; i++) {
|
||||
fl = (struct freelist *)(fla + i * sizes[blktype]);
|
||||
fl->next = (struct freelist *)(fla + (i - 1) * sizes[blktype]);
|
||||
KASSERT(fl != fl->next);
|
||||
}
|
||||
fla = (vaddr_t) fl;
|
||||
fla = (vaddr_t)fl;
|
||||
pr->freelist_offset = fla - prpage;
|
||||
KASSERT(pr->freelist_offset == (pr->nfree-1)*sizes[blktype]);
|
||||
KASSERT(pr->freelist_offset == (pr->nfree - 1) * sizes[blktype]);
|
||||
|
||||
pr->next_samesize = sizebases[blktype];
|
||||
sizebases[blktype] = pr;
|
||||
@ -1021,10 +963,7 @@ subpage_kmalloc(size_t sz
|
||||
* Free a pointer previously returned from subpage_kmalloc. If the
|
||||
* pointer is not on any heap page we recognize, return -1.
|
||||
*/
|
||||
static
|
||||
int
|
||||
subpage_kfree(void *ptr)
|
||||
{
|
||||
static int subpage_kfree(void *ptr) {
|
||||
int blktype; // index into sizes[] that we're using
|
||||
vaddr_t ptraddr; // same as ptr
|
||||
struct pageref *pr; // pageref for page we're freeing in
|
||||
@ -1075,7 +1014,7 @@ subpage_kfree(void *ptr)
|
||||
KASSERT(blktype >= 0 && blktype < NSIZES);
|
||||
|
||||
/* check for corruption */
|
||||
KASSERT(blktype>=0 && blktype<NSIZES);
|
||||
KASSERT(blktype >= 0 && blktype < NSIZES);
|
||||
checksubpage(pr);
|
||||
|
||||
if (ptraddr >= prpage && ptraddr < prpage + PAGE_SIZE) {
|
||||
@ -1083,7 +1022,7 @@ subpage_kfree(void *ptr)
|
||||
}
|
||||
}
|
||||
|
||||
if (pr==NULL) {
|
||||
if (pr == NULL) {
|
||||
/* Not on any of our pages - not a subpage allocation */
|
||||
spinlock_release(&kmalloc_spinlock);
|
||||
return -1;
|
||||
@ -1145,8 +1084,7 @@ subpage_kfree(void *ptr)
|
||||
/* Call free_kpages without kmalloc_spinlock. */
|
||||
spinlock_release(&kmalloc_spinlock);
|
||||
free_kpages(prpage);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
spinlock_release(&kmalloc_spinlock);
|
||||
}
|
||||
|
||||
@ -1166,9 +1104,7 @@ subpage_kfree(void *ptr)
|
||||
* Allocate a block of size SZ. Redirect either to subpage_kmalloc or
|
||||
* alloc_kpages depending on how big SZ is.
|
||||
*/
|
||||
void *
|
||||
kmalloc(size_t sz)
|
||||
{
|
||||
void *kmalloc(size_t sz) {
|
||||
size_t checksz;
|
||||
#ifdef LABELS
|
||||
vaddr_t label;
|
||||
@ -1188,9 +1124,9 @@ kmalloc(size_t sz)
|
||||
vaddr_t address;
|
||||
|
||||
/* Round up to a whole number of pages. */
|
||||
npages = (sz + PAGE_SIZE - 1)/PAGE_SIZE;
|
||||
npages = (sz + PAGE_SIZE - 1) / PAGE_SIZE;
|
||||
address = alloc_kpages(npages);
|
||||
if (address==0) {
|
||||
if (address == 0) {
|
||||
return NULL;
|
||||
}
|
||||
KASSERT(address % PAGE_SIZE == 0);
|
||||
@ -1208,17 +1144,14 @@ kmalloc(size_t sz)
|
||||
/*
|
||||
* Free a block previously returned from kmalloc.
|
||||
*/
|
||||
void
|
||||
kfree(void *ptr)
|
||||
{
|
||||
void kfree(void *ptr) {
|
||||
/*
|
||||
* Try subpage first; if that fails, assume it's a big allocation.
|
||||
*/
|
||||
if (ptr == NULL) {
|
||||
return;
|
||||
} else if (subpage_kfree(ptr)) {
|
||||
KASSERT((vaddr_t)ptr%PAGE_SIZE==0);
|
||||
KASSERT((vaddr_t)ptr % PAGE_SIZE == 0);
|
||||
free_kpages((vaddr_t)ptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user