1 #include "pipe.h"
2 #include <common/spinlock.h>
3 #include <process/process.h>
4 #include <process/ptrace.h>
5 #include <filesystem/vfs/VFS.h>
6 #include <filesystem/fat32/fat32.h>
7 #include <common/atomic.h>
8 #include <mm/slab.h>
9
10 struct pipe_data_t
11 {
12 volatile unsigned int valid_cnt;
13 unsigned int read_pos;
14 unsigned int write_pos;
15 wait_queue_node_t read_wait_queue;
16 wait_queue_node_t write_wait_queue;
17 spinlock_t lock;
18 } __attribute__((packed));
19
20 // 由于kmalloc分配的内存是按照2^n对齐的,因此我们需要这样来确定pipe的buffer大小以消除内部碎片
21 // 我们设定pipe的总大小为1024字节
22 #define PIPE_BUFF_SIZE (1024 - sizeof(struct pipe_data_t))
23
24 struct pipe_t
25 {
26 struct pipe_data_t data;
27 char buf[PIPE_BUFF_SIZE];
28 };
29
pipe_read(struct vfs_file_t * file_ptr,char * buf,int64_t count,long * position)30 long pipe_read(struct vfs_file_t *file_ptr, char *buf,
31 int64_t count, long *position)
32 {
33 int i = 0;
34 struct pipe_t *pipe_ptr = NULL;
35
36 kdebug("pipe_read into!\n");
37 pipe_ptr = (struct pipe_t *)file_ptr->private_data;
38 spin_lock(&pipe_ptr->data.lock);
39 while (pipe_ptr->data.valid_cnt == 0)
40 {
41 /* pipe 空 */
42 kdebug("pipe_read empty!\n");
43 wait_queue_wakeup(&pipe_ptr->data.write_wait_queue, PROC_UNINTERRUPTIBLE);
44 wait_queue_sleep_on_unlock(&pipe_ptr->data.read_wait_queue, (void *)&pipe_ptr->data.lock);
45 spin_lock(&pipe_ptr->data.lock);
46 }
47 for (i = 0; i < pipe_ptr->data.valid_cnt; i++)
48 {
49 if (i == count)
50 {
51 break;
52 }
53 copy_to_user(buf + i, &pipe_ptr->buf[pipe_ptr->data.read_pos], sizeof(char));
54 pipe_ptr->data.read_pos = (pipe_ptr->data.read_pos + 1) % PIPE_BUFF_SIZE;
55 }
56 pipe_ptr->data.valid_cnt = pipe_ptr->data.valid_cnt - i;
57 spin_unlock(&pipe_ptr->data.lock);
58 wait_queue_wakeup(&pipe_ptr->data.write_wait_queue, PROC_UNINTERRUPTIBLE);
59 kdebug("pipe_read end!\n");
60
61 return i;
62 }
pipe_write(struct vfs_file_t * file_ptr,char * buf,int64_t count,long * position)63 long pipe_write(struct vfs_file_t *file_ptr, char *buf,
64 int64_t count, long *position)
65 {
66 int i = 0;
67 struct pipe_t *pipe_ptr = NULL;
68
69 kdebug("pipe_write into!\n");
70 pipe_ptr = (struct pipe_t *)file_ptr->private_data;
71 spin_lock(&pipe_ptr->data.lock);
72 while (pipe_ptr->data.valid_cnt + count >= PIPE_BUFF_SIZE)
73 {
74 /* pipe 满 */
75 kdebug("pipe_write pipe full!\n");
76 wait_queue_wakeup(&pipe_ptr->data.read_wait_queue, PROC_UNINTERRUPTIBLE);
77 wait_queue_sleep_on_unlock(&pipe_ptr->data.write_wait_queue, (void *)&pipe_ptr->data.lock);
78 spin_lock(&pipe_ptr->data.lock);
79 }
80 for (i = pipe_ptr->data.valid_cnt; i < PIPE_BUFF_SIZE; i++)
81 {
82 if (i - pipe_ptr->data.valid_cnt == count)
83 {
84 break;
85 }
86 copy_from_user(&pipe_ptr->buf[pipe_ptr->data.write_pos], buf + i, sizeof(char));
87 pipe_ptr->data.write_pos = (pipe_ptr->data.write_pos + 1) % PIPE_BUFF_SIZE;
88 }
89 pipe_ptr->data.valid_cnt += count;
90 spin_unlock(&pipe_ptr->data.lock);
91 wait_queue_wakeup(&pipe_ptr->data.read_wait_queue, PROC_UNINTERRUPTIBLE);
92 kdebug("pipe_write out!\n");
93
94 return count;
95 }
96
pipe_close(struct vfs_index_node_t * inode,struct vfs_file_t * file_ptr)97 long pipe_close(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
98 {
99 return 0;
100 }
101
102 struct vfs_file_operations_t g_pipe_file_ops = {
103 .open = NULL,
104 .close = pipe_close,
105 .read = pipe_read,
106 .write = pipe_write,
107 .lseek = NULL,
108 .ioctl = NULL,
109 .readdir = NULL,
110 };
111
pipe_alloc()112 static struct pipe_t *pipe_alloc()
113 {
114 struct pipe_t *pipe_ptr = NULL;
115
116 pipe_ptr = (struct pipe_t *)kzalloc(sizeof(struct pipe_t), 0);
117 spin_init(&pipe_ptr->data.lock);
118 pipe_ptr->data.read_pos = 0;
119 pipe_ptr->data.write_pos = 0;
120 pipe_ptr->data.valid_cnt = 0;
121 memset(pipe_ptr->buf, 0, PIPE_BUFF_SIZE);
122 wait_queue_init(&pipe_ptr->data.read_wait_queue, NULL);
123 wait_queue_init(&pipe_ptr->data.write_wait_queue, NULL);
124
125 return pipe_ptr;
126 }
127
128 /**
129 * @brief 创建管道
130 *
131 * @param fd(r8) 文件句柄指针
132 * @param num(r9) 文件句柄个数
133 * @return uint64_t
134 */
sys_pipe(struct pt_regs * regs)135 uint64_t sys_pipe(struct pt_regs *regs)
136 {
137 int *fd = NULL;
138 struct pipe_t *pipe_ptr = NULL;
139 struct vfs_file_t *read_file = NULL;
140 struct vfs_file_t *write_file = NULL;
141
142 fd = (int *)regs->r8;
143 kdebug("pipe creat into!\n");
144 /* step1 申请pipe结构体、初始化 */
145 pipe_ptr = pipe_alloc();
146 /* step2 申请2个fd文件句柄,1个作为读端、1个作为写端 */
147 read_file = (struct vfs_file_t *)kzalloc(sizeof(struct vfs_file_t), 0);
148 fd[0] = process_fd_alloc(read_file);
149 if (fd[0] == -1)
150 {
151 kdebug("pipe alloc read fd fail!\n");
152 kfree(pipe_ptr);
153 kfree(read_file);
154 return -1;
155 }
156 write_file = (struct vfs_file_t *)kzalloc(sizeof(struct vfs_file_t), 0);
157 fd[1] = process_fd_alloc(write_file);
158 if (fd[1] == -1)
159 {
160 kdebug("pipe alloc write fd fail!\n");
161 kfree(pipe_ptr);
162 kfree(read_file);
163 kfree(write_file);
164 return -1;
165 }
166 /* step3 绑定pipe和file */
167 read_file->private_data = (void *)pipe_ptr;
168 read_file->file_ops = &g_pipe_file_ops;
169 read_file->mode = VFS_FILE_MODE_READ;
170 write_file->private_data = (void *)pipe_ptr;
171 write_file->file_ops = &g_pipe_file_ops;
172 write_file->mode = VFS_FILE_MODE_WRITE;
173 kdebug("pipe creat end!\n");
174
175 return 0;
176 }
177