Working filesystem.
[pintos-anon] / src / filesys / filesys-stub.c
1 #include "filesys-stub.h"
2 #include <stdarg.h>
3 #include "backdoor.h"
4 #include "debug.h"
5 #include "io.h"
6 #include "lib.h"
7 #include "synch.h"
8
9 static struct lock lock;
10
11 void 
12 filesys_stub_init (void)
13 {
14   lock_init (&lock, "filesys-stub");
15 }
16
17 void 
18 filesys_stub_lock (void)
19 {
20   lock_acquire (&lock);
21 }
22
23 void 
24 filesys_stub_unlock (void)
25 {
26   lock_release (&lock);
27 }
28 \f
29 static void
30 out_byte (uint8_t byte, void *aux UNUSED) 
31 {
32   outb (0x8901, byte);
33 }
34
35 void 
36 filesys_stub_put_bool (bool b)
37 {
38   backdoor_put_bool (b, out_byte, NULL);
39 }
40
41 void 
42 filesys_stub_put_bytes (const void *buffer, size_t cnt)
43 {
44   backdoor_put_bytes (buffer, cnt, out_byte, NULL);
45 }
46
47 void 
48 filesys_stub_put_file (struct file *file)
49 {
50   ASSERT (file != NULL);
51   filesys_stub_put_int32 ((int32_t) file - 1);
52 }
53
54 void 
55 filesys_stub_put_int32 (int32_t value)
56 {
57   backdoor_put_int32 (value, out_byte, NULL);
58 }
59
60 void 
61 filesys_stub_put_string (const char *string)
62 {
63   backdoor_put_string (string, out_byte, NULL);
64 }
65
66 void 
67 filesys_stub_put_uint32 (uint32_t value)
68 {
69   backdoor_put_uint32 (value, out_byte, NULL);
70 }
71
72 static uint8_t
73 in_byte (void *aux UNUSED) 
74 {
75   return inb (0x8901);
76 }
77
78 bool 
79 filesys_stub_get_bool (void)
80 {
81   return backdoor_get_bool (in_byte, NULL);
82 }
83
84 void 
85 filesys_stub_get_bytes (void *buffer, size_t size)
86 {
87   /* We could use backdoor_get_bytes() but this is significantly
88      faster. */
89   asm ("rep insl; movl %0, %%ecx; rep insb"
90        :
91        : "g" (size % 4), "d" (0x8901), "c" (size / 4), "D" (buffer));
92 }
93
94 struct file *
95 filesys_stub_get_file (void)
96 {
97   int32_t fd = filesys_stub_get_int32 ();
98   return fd < 0 ? NULL : (struct file *) (fd + 1);
99 }
100
101 int32_t 
102 filesys_stub_get_int32 (void)
103 {
104   return backdoor_get_int32 (in_byte, NULL);
105 }
106
107 void 
108 filesys_stub_match_string (const char *string)
109 {
110   if (backdoor_get_uint32 (in_byte, NULL) != strlen (string))
111     panic ("string match failed");
112   while (*string != '\0') 
113     {
114       uint8_t c = *string++;
115       if (c != in_byte (NULL))
116         panic ("string match failed"); 
117     }
118 }
119
120 uint32_t 
121 filesys_stub_get_uint32 (void)
122 {
123   return backdoor_get_uint32 (in_byte, NULL);
124 }
125