PatchworkOS  dbbdc99
A non-POSIX operating system.
Loading...
Searching...
No Matches
aml.c
Go to the documentation of this file.
2
3#include <kernel/log/log.h>
4#include <kernel/log/panic.h>
11#include <kernel/acpi/tables.h>
12
13#include <kernel/log/log.h>
14
15#include <errno.h>
16#include <sys/math.h>
17
19
20static inline uint64_t aml_parse(const uint8_t* start, const uint8_t* end)
21{
22 if (start == NULL || end == NULL || start > end)
23 {
24 errno = EINVAL;
25 return ERR;
26 }
27
28 if (start == end)
29 {
30 // Im not sure why but some firmwares have empty SSDTs.
31 return 0;
32 }
33
34 // In section 20.2.1, we see the definition AMLCode := DefBlockHeader TermList.
35 // The DefBlockHeader is already read as thats the `sdt_header_t`.
36 // So the entire code is a termlist.
37
38 aml_state_t state;
39 if (aml_state_init(&state, NULL) == ERR)
40 {
41 return ERR;
42 }
43
46
47 uint64_t result = aml_term_list_read(&state, root, start, end, NULL);
48
49 if (result != ERR)
50 {
52 }
53
54 aml_state_deinit(&state);
55 return result;
56}
57
58static inline uint64_t aml_init_parse_all(void)
59{
61 if (dsdt == NULL)
62 {
63 LOG_ERR("failed to retrieve DSDT\n");
64 return ERR;
65 }
66
67 LOG_INFO("DSDT found containing %llu bytes of AML code\n", dsdt->header.length - sizeof(dsdt_t));
68
69 const uint8_t* dsdtEnd = (const uint8_t*)dsdt + dsdt->header.length;
70 if (aml_parse(dsdt->definitionBlock, dsdtEnd) == ERR)
71 {
72 LOG_ERR("failed to parse DSDT\n");
73 return ERR;
74 }
75
76 uint64_t index = 0;
77 ssdt_t* ssdt = NULL;
78 while (true)
79 {
80 ssdt = (ssdt_t*)acpi_tables_lookup(SSDT_SIGNATURE, sizeof(ssdt_t), index);
81 if (ssdt == NULL)
82 {
83 break;
84 }
85
86 LOG_INFO("SSDT%llu found containing %llu bytes of AML code\n", index, ssdt->header.length - sizeof(ssdt_t));
87
88 const uint8_t* ssdtEnd = (const uint8_t*)ssdt + ssdt->header.length;
89 if (aml_parse(ssdt->definitionBlock, ssdtEnd) == ERR)
90 {
91 LOG_ERR("failed to parse SSDT%llu\n", index);
92 return ERR;
93 }
94
95 index++;
96 }
97
98 LOG_INFO("parsed 1 DSDT and %llu SSDTs\n", index);
99
100 return 0;
101}
102
104{
105 LOG_INFO("AML revision %d, init and parse all\n", AML_CURRENT_REVISION);
106
109
111 if (root == NULL)
112 {
113 LOG_ERR("failed to create root AML object\n");
114 return ERR;
115 }
117
118 // We dont need to add the root to the namespace map as it has no name.
120 {
121 LOG_ERR("failed to set predefined scope for root object\n");
122 return ERR;
123 }
124
126
128 {
129 LOG_ERR("failed to initialize AML integer handling\n");
130 return ERR;
131 }
132
133 if (aml_predefined_init() == ERR)
134 {
135 LOG_ERR("failed to initialize AML predefined names\n");
136 return ERR;
137 }
138
139 if (aml_patch_up_init() == ERR)
140 {
141 LOG_ERR("failed to initialize AML patch up\n");
142 return ERR;
143 }
144
145 if (aml_init_parse_all() == ERR)
146 {
147 LOG_ERR("failed to parse all AML code\n");
148 return ERR;
149 }
150
151 LOG_INFO("resolving %llu unresolved objects\n", aml_patch_up_unresolved_count());
153 {
154 LOG_ERR("failed to resolve all unresolved objects\n");
155 return ERR;
156 }
157
159 {
160 LOG_ERR("there are still %llu unresolved objects after patch up\n", aml_patch_up_unresolved_count());
161 return ERR;
162 }
163
164 return 0;
165}
166
168{
169 return &bigMutex;
170}
static uint64_t aml_parse(const uint8_t *start, const uint8_t *end)
Definition aml.c:20
static uint64_t aml_init_parse_all(void)
Definition aml.c:58
static mutex_t bigMutex
Definition aml.c:18
static void start()
Definition main.c:542
static dentry_t * root
Definition devfs.c:25
uint64_t aml_term_list_read(aml_state_t *state, aml_object_t *scope, const uint8_t *start, const uint8_t *end, aml_term_list_ctx_t *parentCtx)
Reads a TermList structure from the AML byte stream.
Definition term.c:188
uint64_t aml_integer_handling_init(void)
Initialize integer handling.
Definition integer.c:8
uint64_t aml_namespace_commit(aml_overlay_t *overlay)
Commit all names in a namespace overlay to the global namespace heirarchy.
Definition namespace.c:506
void aml_namespace_init(aml_object_t *root)
Initialize the namespace heirarchy.
Definition namespace.c:74
aml_object_t * aml_namespace_get_root(void)
Get the root object of the namespace heirarchy.
Definition namespace.c:149
aml_object_t * aml_object_new(void)
Allocate a new ACPI object.
Definition object.c:51
uint64_t aml_predefined_scope_set(aml_object_t *object)
Set a object as a predefined scope with the given name.
Definition object.c:1199
uint64_t aml_patch_up_init(void)
Initialize the patch-up system.
Definition patch_up.c:15
uint64_t aml_patch_up_unresolved_count(void)
Get the number of unresolved references in the global list.
Definition patch_up.c:105
uint64_t aml_patch_up_resolve_all(void)
Attempts to resolve all unresolved references.
Definition patch_up.c:60
uint64_t aml_predefined_init(void)
Initialize predefined AML names and objects.
Definition predefined.c:117
uint64_t aml_state_init(aml_state_t *state, aml_object_t **args)
Initialize an AML state.
Definition state.c:8
void aml_state_deinit(aml_state_t *state)
Deinitialize an AML state.
Definition state.c:68
uint64_t aml_init(void)
Initialize the AML subsystem.
Definition aml.c:103
mutex_t * aml_big_mutex_get(void)
Get the mutex for the entire AML subsystem.
Definition aml.c:167
#define AML_CURRENT_REVISION
The current revision of the AML subsystem.
Definition aml.h:46
sdt_header_t * acpi_tables_lookup(const char *signature, uint64_t minSize, uint64_t n)
Lookup the n'th table matching the signature.
Definition tables.c:259
#define DSDT_SIGNATURE
DSDT table signature.
Definition tables.h:219
#define SSDT_SIGNATURE
SSDT table signature.
Definition tables.h:238
#define LOG_ERR(format,...)
Definition log.h:93
#define LOG_INFO(format,...)
Definition log.h:91
void mutex_init(mutex_t *mtx)
Initializes a mutex.
Definition mutex.c:14
#define MUTEX_SCOPE(mutex)
Acquires a mutex for the reminder of the current scope.
Definition mutex.h:23
#define UNREF_DEFER(ptr)
RAII-style cleanup for scoped references.
Definition ref.h:122
#define EINVAL
Invalid argument.
Definition errno.h:142
#define errno
Error number variable.
Definition errno.h:27
#define NULL
Pointer error value.
Definition NULL.h:25
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
ACPI object.
Definition object.h:447
AML State.
Definition state.h:25
aml_overlay_t overlay
Holds any named objects created during parsing.
Definition state.h:30
Differentiated System Description Table.
Definition tables.h:211
uint8_t definitionBlock[]
Definition tables.h:213
sdt_header_t header
Definition tables.h:212
Mutex structure.
Definition mutex.h:46
uint32_t length
Definition acpi.h:92
Secondary System Description Table.
Definition tables.h:228
uint8_t definitionBlock[]
Definition tables.h:230
sdt_header_t header
Definition tables.h:229