aboutsummaryrefslogtreecommitdiffstats
path: root/com32/modules/meminfo.c
blob: 00d0e14d5ca1160f44d7e14502c047fce532a5a8 (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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2008 H. Peter Anvin - All Rights Reserved
 *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
 *
 *   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, Inc., 51 Franklin St, Fifth Floor,
 *   Boston MA 02110-1301, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * meminfo.c
 *
 * Dump the memory map of the system
 */
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <console.h>
#include <com32.h>

struct e820_data {
    uint64_t base;
    uint64_t len;
    uint32_t type;
    uint32_t extattr;
} __attribute__ ((packed));

static const char *const e820_types[] = {
    "usable",
    "reserved",
    "ACPI reclaim",
    "ACPI NVS",
    "unusable",
};

static void dump_e820(void)
{
    com32sys_t ireg, oreg;
    struct e820_data ed;
    uint32_t type;
    void *low_ed;

	low_ed = lmalloc(sizeof ed);
	if (!low_ed)
		return;

    memset(&ireg, 0, sizeof ireg);

    ireg.eax.w[0] = 0xe820;
    ireg.edx.l = 0x534d4150;
    ireg.ecx.l = sizeof(struct e820_data);
    ireg.edi.w[0] = OFFS(low_ed);
    ireg.es = SEG(low_ed);

    memset(&ed, 0, sizeof ed);
    ed.extattr = 1;

    do {
	memcpy(low_ed, &ed, sizeof ed);

	__intcall(0x15, &ireg, &oreg);
	if (oreg.eflags.l & EFLAGS_CF ||
	    oreg.eax.l != 0x534d4150 || oreg.ecx.l < 20)
	    break;

	memcpy(&ed, low_ed, sizeof ed);

	if (oreg.ecx.l >= 24) {
	    /* ebx base length end type */
	    printf("%8x %016llx %016llx %016llx %d [%x]",
		   ireg.ebx.l, ed.base, ed.len, ed.base + ed.len, ed.type,
		   ed.extattr);
	} else {
	    /* ebx base length end */
	    printf("%8x %016llx %016llx %016llx %d [-]",
		   ireg.ebx.l, ed.base, ed.len, ed.base + ed.len, ed.type);
	    ed.extattr = 1;
	}

	type = ed.type - 1;
	if (type < sizeof(e820_types) / sizeof(e820_types[0]))
	    printf(" %s", e820_types[type]);

	putchar('\n');

	ireg.ebx.l = oreg.ebx.l;
    } while (ireg.ebx.l);

    free(low_ed);
}

static void dump_legacy(void)
{
    com32sys_t ireg, oreg;
    uint16_t dosram = *(uint16_t *) 0x413;
    struct {
	uint16_t offs, seg;
    } *const ivt = (void *)0;

    memset(&ireg, 0, sizeof ireg);

    __intcall(0x12, &ireg, &oreg);

    printf
	("INT 15h = %04x:%04x  DOS RAM: %dK (0x%05x)  INT 12h: %dK (0x%05x)\n",
	 ivt[0x15].seg, ivt[0x15].offs, dosram, dosram << 10, oreg.eax.w[0],
	 oreg.eax.w[0] << 10);

    ireg.eax.b[1] = 0x88;
    __intcall(0x15, &ireg, &oreg);

    printf("INT 15 88: 0x%04x (%uK)  ", oreg.eax.w[0], oreg.eax.w[0]);

    ireg.eax.w[0] = 0xe801;
    __intcall(0x15, &ireg, &oreg);

    printf("INT 15 E801: 0x%04x (%uK) 0x%04x (%uK)\n",
	   oreg.ecx.w[0], oreg.ecx.w[0], oreg.edx.w[0], oreg.edx.w[0] << 6);
}

int main(int argc, char **argv)
{
    dump_legacy();
    dump_e820();
    return 0;
}