nckernel  0.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
zone.c
Go to the documentation of this file.
1 #include <sys/types.h>
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <stddef.h>
5 #include <stdlib.h>
6 #include <errno.h>
7 #include <unistd.h>
8 
9 #include <list.h>
10 #include <zone.h>
11 #include <cpu_node.h>
12 #include <page_frame.h>
13 #include <page_allocator.h>
14 
15 #include <debug.h>
16 
17 static LIST_HEAD(s_zone_list);
18 
19 int create_zone(struct node *node, enum zone_type type, off_t base, size_t size)
20 {
21  struct zone *zone;
22  int ret = 0;
23 
24  zone = malloc(sizeof(*zone));
25  if (!zone) {
26  return -ENOMEM;
27  }
28 
29  zone->node = node;
30  zone->type = type;
31  INIT_LIST_HEAD(&zone->page_list);
32  INIT_LIST_HEAD(&zone->managed_page_list);
33 
34  ret = page_frame_init(zone, (void *)base, (unsigned int)size);
35  if (ret < 0) {
36  dbg_printf("Failed to init zone %x - %d\n", base, size);
37  free(zone);
38  } else {
39  list_add_tail(&zone->head, &s_zone_list);
40  }
41 
42  return ret;
43 }
44 
46 {
47  struct zone *zone;
48  struct list_head *pos;
49 
50  list_for_each(pos, &s_zone_list) {
51  zone = list_entry(pos, struct zone, head);
52  if (zone->type == type) {
53  return zone;
54  }
55  }
56 
57  return NULL;
58 }
59 
60 struct zone *find_zone_by_addr(void *pma)
61 {
62  struct zone *zone;
63  struct list_head *pos;
64  struct list_head *npos;
65  struct page_frame *frame;
66  unsigned int start;
67  unsigned int end;
68 
69  list_for_each(pos, &s_zone_list) {
70  zone = list_entry(pos, struct zone, head);
71  if (page_allocator_get_region(zone->handle, &start, &end) < 0) {
72  continue;
73  }
74 
75  if (start <= (off_t)pma && (off_t)pma < end) {
76  return zone;
77  }
78  }
79 
80  list_for_each(pos, &s_zone_list) {
81  zone = list_entry(pos, struct zone, head);
82  list_for_each(npos, &zone->managed_page_list) {
83  frame = list_entry(npos, struct page_frame, page_head);
84  if (frame->pma == pma) {
85  return zone;
86  }
87  }
88  }
89 
90  return NULL;
91 }
92 
93 int destroy_zone(struct zone *zone)
94 {
95  list_del(&zone->head);
96  page_frame_fini(zone);
97  free(zone);
98  return 0;
99 }
100 
101 /* End of a file */