Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
9pingg committed Jun 22, 2022
1 parent d3175b1 commit 88ad581
Show file tree
Hide file tree
Showing 28 changed files with 247,144 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added diff/.DS_Store
Binary file not shown.
122,571 changes: 122,571 additions & 0 deletions diff/diff_after_compiling.txt

Large diffs are not rendered by default.

122,571 changes: 122,571 additions & 0 deletions diff/patch_file.patch

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions new_device/.Module.symvers.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmd_/home/kern/project_os/2020260_Assignment04/new_device/Module.symvers := sed 's/\.ko$$/\.o/' /home/kern/project_os/2020260_Assignment04/new_device/modules.order | scripts/mod/modpost -a -o /home/kern/project_os/2020260_Assignment04/new_device/Module.symvers -e -i Module.symvers -N -T -
1 change: 1 addition & 0 deletions new_device/.modules.order.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmd_/home/kern/project_os/2020260_Assignment04/new_device/modules.order := { echo /home/kern/project_os/2020260_Assignment04/new_device/new_device.ko; :; } | awk '!x[$$0]++' - > /home/kern/project_os/2020260_Assignment04/new_device/modules.order
1 change: 1 addition & 0 deletions new_device/.new_device.ko.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmd_/home/kern/project_os/2020260_Assignment04/new_device/new_device.ko := ld -r -m elf_x86_64 --build-id=sha1 -T scripts/module.lds -o /home/kern/project_os/2020260_Assignment04/new_device/new_device.ko /home/kern/project_os/2020260_Assignment04/new_device/new_device.o /home/kern/project_os/2020260_Assignment04/new_device/new_device.mod.o; true
1 change: 1 addition & 0 deletions new_device/.new_device.mod.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmd_/home/kern/project_os/2020260_Assignment04/new_device/new_device.mod := { echo /home/kern/project_os/2020260_Assignment04/new_device/new_device.o; echo; } > /home/kern/project_os/2020260_Assignment04/new_device/new_device.mod
692 changes: 692 additions & 0 deletions new_device/.new_device.mod.o.cmd

Large diffs are not rendered by default.

988 changes: 988 additions & 0 deletions new_device/.new_device.o.cmd

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions new_device/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
obj-m = new_device.o

KBUILD_CFLAGS += -w

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules $(KBUILD_CFLAGS)
sudo insmod new_device.ko buffer_size=200
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Empty file added new_device/Module.symvers
Empty file.
1 change: 1 addition & 0 deletions new_device/modules.order
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/home/kern/project_os/2020260_Assignment04/new_device/new_device.ko
151 changes: 151 additions & 0 deletions new_device/new_device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/rtc.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/semaphore.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>


#define LICENSE "GPL"

MODULE_LICENSE(LICENSE);

static struct miscdevice my_device;
static struct semaphore full;
static struct semaphore empty;
static struct semaphore _read;
static struct semaphore _write;

static int no_of_open_devices;
static int buffer_size;
static int bytes_string = 200;
static int index_r = 0;
static int index_w = 0;
static int buf_clear;
static struct semaphore full;
static struct semaphore empty;
static struct semaphore _read;
static struct semaphore _write;
char** buffer;

module_param(buffer_size, int, 0000);

static ssize_t dequeue_data(struct file*, char*, size_t, loff_t*);
static ssize_t enqueue_data(struct file*, const char*, size_t, loff_t*);
static int open_device(struct inode*, struct file*);
static int close_device(struct inode*, struct file*);
static struct file_operations my_device_fops = {
.open = &open_device,
.read = &dequeue_data,
.write = &enqueue_data,
.release = &close_device
};

int init_module(){
my_device.name = "new_device";
my_device.fops = &my_device_fops;
my_device.minor = MISC_DYNAMIC_MINOR;
int res;
if((res = misc_register(&my_device))){
printk(KERN_ERR "Device new_device cannot be registered\n");
return res;
}
printk(KERN_INFO "New Device Registered:\n");
printk(KERN_INFO "device name: new_device\n");
printk(KERN_INFO "buffer size: %d\n", buffer_size);
sema_init(&_read, 1);
sema_init(&_write, 1);
sema_init(&full, 0);
sema_init(&empty, buffer_size);
int data_allocated = 0;

buffer = (char**)kmalloc(buffer_size * sizeof(char*), GFP_KERNEL);

while(data_allocated < buffer_size)
{
buffer[data_allocated] = (char*)kmalloc((bytes_string + 1) * sizeof(char), GFP_KERNEL);
buffer[bytes_string] = '\0';
++data_allocated;
}
no_of_open_devices = 0;
buf_clear = buffer_size;
return 0;
}

static int open_device(struct inode* _inode, struct file* fp){
++no_of_open_devices;
return 0;
}
static int close_device(struct inode* _inode, struct file* fp){
--no_of_open_devices;
return 0;
}
static ssize_t dequeue_data(struct file* fp, char* user_buffer, size_t char_read_size, loff_t* pos){
int i = 0;
down_interruptible(&_read);
down_interruptible(&full);
index_r %= buffer_size;
for(i = 0; i < char_read_size; i++){
if(buf_clear >= buffer_size){
break;
}
copy_to_user(&user_buffer[i], &buffer[index_r][i], 1);
}
++buf_clear;
++index_r;
up(&empty);
up(&_read);
return i;
}

static ssize_t enqueue_data(struct file* fp, const char* user_buffer, size_t char_write_size, loff_t* pos){
int i = 0;
down_interruptible(&_write);
down_interruptible(&empty);
index_w %= buffer_size;
for(i = 0; i < char_write_size; i++)
{
if(buf_clear <= 0)
{
break;
}
copy_from_user(&buffer[index_w][i], &user_buffer[i], 1);
}
--buf_clear;
++index_w;
// while(1){
// result = syscall(READER_SYSCALL_NO, random_str, BYTE_COUNT);
// //result = read(fd1, random_str, sizeof(random_str));
// if(result < 0){
// perror("syscall");

// }
// if(result >= 0){
// printf("%d\n", result);
// printf("******************************************\n\nstring received: %d %s\n\n", cnt,random_str);
// cnt++;
// //memset(random_str, 0, 8);

// }
// sleep(2);
// }
up(&full);
up(&_write);
return i;
}


void cleanup_module(){
int free_space;
for(free_space = 0; free_space < buffer_size; free_space++){
kfree(buffer[free_space]);
}
misc_deregister(&my_device);
printk(KERN_INFO "Device Unregistered: new_device\n");
}
Binary file added new_device/new_device.ko
Binary file not shown.
2 changes: 2 additions & 0 deletions new_device/new_device.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/home/kern/project_os/2020260_Assignment04/new_device/new_device.o

31 changes: 31 additions & 0 deletions new_device/new_device.mod.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <linux/module.h>
#define INCLUDE_VERMAGIC
#include <linux/build-salt.h>
#include <linux/elfnote-lto.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>

BUILD_SALT;
BUILD_LTO_INFO;

MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);

__visible struct module __this_module
__section(".gnu.linkonce.this_module") = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};

#ifdef CONFIG_RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif

MODULE_INFO(depends, "");


MODULE_INFO(srcversion, "E7F873D52D59CFDD69475ED");
Binary file added new_device/new_device.mod.o
Binary file not shown.
Binary file added new_device/new_device.o
Binary file not shown.
Binary file added system calls/.DS_Store
Binary file not shown.
28 changes: 28 additions & 0 deletions system calls/systemcall_kernelread
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
SYSCALL_DEFINE2(kernel_device_reader, char __user *, read_buffer, int, len_buffer)
+{
+ struct file * fp;
+ //mm_segment_t fs;
+ loff_t pos;
+ //char buf[8];
+ //int fd = ksys_open("/dev/new_device", O_RDWR);
+ fp = filp_open("/dev/new_device", O_RDWR , 0777);
+ if (IS_ERR (fp)) {
+ printk ("file error\n");
+ return -EFAULT;
+ }
+ pos = 0;
+ ssize_t result = vfs_read (fp, read_buffer, sizeof (read_buffer), & pos);
+ //int result = ksys_read(fd, buf, len_buffer);
+ if(result < 0){
+ printk("read error");
+ return -EFAULT;
+ }
+ // long bytes_copied = copy_to_user(read_buffer, buf, len_buffer);
+ // if(bytes_copied < 0){
+ // printk("copy to user error");
+ // return -EFAULT;
+ // }
+ //ksys_close(fd);
+ filp_close (fp, NULL);
+ return 0;
+}
32 changes: 32 additions & 0 deletions system calls/systemcall_kernelwrite
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
+SYSCALL_DEFINE2(kernel_device_writer, char __user *, write_buffer, int, len_buffer)
+{
+ //char buf[8];
+ struct file * fp;
+ //mm_segment_t fs;
+ loff_t pos;
+ // int fd = ksys_open("/dev/new_device", O_WRONLY);
+ // if(fd== -1){
+ // printk("writer open error");
+ // return -EFAULT;
+ // }
+ fp = filp_open("/dev/new_device", O_WRONLY , 0777);
+ if (IS_ERR (fp)) {
+ printk ("file error\n");
+ return -EFAULT;
+ }
+ // long bytes_copied = strncpy_from_user(buf, write_buffer, len_buffer);
+ // if(bytes_copied < 0){
+ // printk("strcpy writer error");
+ // return -EFAULT;
+ // }
+ pos = 0;
+ ssize_t result = vfs_write (fp, write_buffer, sizeof (write_buffer), & pos);
+ //int result = ksys_write(fd, buf, len_buffer);
+ if(result < 0){
+ printk("write error");
+ return -EFAULT;
+ }
+ //ksys_close(fd);
+ filp_close (fp, NULL);
+ return 0;
+}
Binary file added test/.DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
deafult:
gcc consumer.c -o c
gcc producer.c -o p
clean:
rm c p
Binary file added test/c
Binary file not shown.
25 changes: 25 additions & 0 deletions test/consumer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define BYTE_COUNT 8
#define READER_SYSCALL_NO 448
int main(void){
char random_str[8];
int result = 0;
int cnt = 1;
while(1){
result = syscall(READER_SYSCALL_NO, random_str, BYTE_COUNT);
if(result < 0){
perror("syscall");
}
if(result >= 0){
printf("******************************************\n\nstring received: %d %s\n\n", cnt,random_str);
cnt++;
}
sleep(2);
}
return 0;
}
//buffer_size=20
Binary file added test/p
Binary file not shown.
34 changes: 34 additions & 0 deletions test/producer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#define BYTE_COUNT 8
#define WRITER_SYSCALL_NO 449
int main(void){
int cnt = 1;
int fd = open("/dev/urandom", O_RDONLY);
if(fd== -1){
perror("open");
exit(1);
}
char random_str[8];
while(1){
ssize_t res = read(fd, random_str, 8);
if(res < 0){
perror("read");
exit(1);
}
int result = syscall(WRITER_SYSCALL_NO, random_str, BYTE_COUNT);
if(result < 0){
perror("syscall");
exit(1);
}
if(result >= 0){
printf("******************************************\n\nstring sent:%d %s\n\n", cnt,random_str);
cnt++;
}
sleep(2);
}
close(fd);
return 0;
}

0 comments on commit 88ad581

Please sign in to comment.