summaryrefslogtreecommitdiff
path: root/kern/printf.c
blob: 8ef3e8631f48b9e29e0ccc212e9d577fc05da42a (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
/*
 * Copyright (c) 2010-2019 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>

#include <kern/console.h>
#include <kern/fmt.h>
#include <kern/init.h>
#include <kern/spinlock.h>
#include <machine/boot.h>
#include <machine/cpu.h>

/*
 * Size of the static buffer.
 */
#define PRINTF_BUFSIZE 1024

static char printf_buffer[PRINTF_BUFSIZE];
static struct spinlock printf_lock;

static int
vprintf_common(const char *format, va_list ap)
{
    int length;

    length = fmt_vsnprintf(printf_buffer, sizeof(printf_buffer), format, ap);

    for (char *ptr = printf_buffer; *ptr != '\0'; ptr++) {
        console_putchar(*ptr);
    }

    return length;
}

int
printf(const char *format, ...)
{
    va_list ap;
    int length;

    va_start(ap, format);
    length = vprintf(format, ap);
    va_end(ap);

    return length;
}

int
vprintf(const char *format, va_list ap)
{
    unsigned long flags;
    int length;

    spinlock_lock_intr_save(&printf_lock, &flags);
    length = vprintf_common(format, ap);
    spinlock_unlock_intr_restore(&printf_lock, flags);

    return length;
}

int
printf_ln(const char *format, ...)
{
    va_list ap;
    int length;

    va_start(ap, format);
    length = vprintf_ln(format, ap);
    va_end(ap);

    return length;
}

int
vprintf_ln(const char *format, va_list ap)
{
    unsigned long flags;
    int length;

    spinlock_lock_intr_save(&printf_lock, &flags);
    length = vprintf_common(format, ap);
    console_putchar('\n');
    spinlock_unlock_intr_restore(&printf_lock, flags);

    return length;
}

static int __init
printf_setup(void)
{
    spinlock_init(&printf_lock);
    return 0;
}

INIT_OP_DEFINE(printf_setup,
               INIT_OP_DEP(boot_bootstrap_console, true),
               INIT_OP_DEP(console_bootstrap, true),
               INIT_OP_DEP(spinlock_setup, true));