Skip to content

Instantly share code, notes, and snippets.

@juniorprincewang
juniorprincewang / no-madvise.c
Created December 26, 2019 06:24 — forked from niedbalski/no-madvise.c
madvise tests
niedbalski@theos-mobile:~$ cat test-madvise.c
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(void) {
size_t size = sysconf(_SC_PAGE_SIZE) * 6; //24K
@juniorprincewang
juniorprincewang / a0c0-counter.c
Created February 21, 2020 14:16
NVIDIA NVE4 compute process using MMIO
//from https://people.freedesktop.org/~chrisbmr/a0c0-counter.c
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <unistd.h>
#include <assert.h>
@juniorprincewang
juniorprincewang / 90c0.c
Created February 21, 2020 14:22
NVIDIA NVC0 compute process using MMIO
// https://people.freedesktop.org/~chrisbmr/90c0.c
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <libdrm/nouveau.h>
@juniorprincewang
juniorprincewang / pci-driver.c
Created March 10, 2020 08:41
an example of a simple pci driver for a VIRTUAL card
// http://www.zarb.org/~trem/kernel/pci/pci-driver.c
/***************************************************************************
* Copyright (C) 2005 by trem *
* [email protected] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
@juniorprincewang
juniorprincewang / Makefile
Created March 19, 2020 06:36
kernel module for `get_user_pages_remote()`
obj-m += sample.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
@juniorprincewang
juniorprincewang / Makefile
Last active March 28, 2020 07:31
Glib Hash Table
all:
gcc -I/usr/include/glib-2.0/ ghashtable.c -o main -lglib-2.0
clean:
rm -rf main
@juniorprincewang
juniorprincewang / setting.sh
Created April 1, 2020 12:52
CUDA PATH and LD_LIBRARY_PATH setting
echo 'export PATH=$PATH:/usr/local/cuda/bin' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib:/usr/local/cuda/lib64' >> ~/.bashrc
source ~/.bashrc
sudo bash -c "echo /usr/local/cuda/lib64/ > /etc/ld.so.conf.d/cuda.conf"
sudo ldconfig
@juniorprincewang
juniorprincewang / cuda_version.sh
Created April 5, 2020 10:33
Get cuda version
#!/bin/bash
version="$(cat /usr/local/cuda/version.txt | head -n1|cut -d " " -f3)"
echo $version
majorminor=${version%.*}
echo $majorminor
@juniorprincewang
juniorprincewang / awesome.patch
Created April 16, 2020 01:56 — forked from 17twenty/awesome.patch
Really simple example of using get_user_pages and memory aligned allocation using posix_memalign. Hope this helps!
diff -PurN dummy/gu_page.c get_user_pages/gu_page.c
--- dummy/gu_page.c 1970-01-01 01:00:00.000000000 +0100
+++ get_user_pages/gu_page.c 2012-06-14 14:41:31.797310260 +0100
@@ -0,0 +1,124 @@
+#include <linux/module.h>
+#include <linux/mm.h>
+#include <linux/sched.h>
+#include <linux/slab.h>
+#include <linux/vmalloc.h>
+
@juniorprincewang
juniorprincewang / vm_insert_page.c
Last active April 24, 2020 12:42
vm_insert_page
int _mmap(struct file *f, struct vm_area_struct *vma) {
unsigned long pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
unsigned long addr;
struct page *page;
int i,rc=-ENODEV;
// TODO need any semaphore for vma manipulation?
printk(KERN_DEBUG "vma->vm_end %lu vm_start %lu len %lu pages %lu vm_pgoff %lu\n",
vma->vm_end, vma->vm_start, vma->vm_end - vma->vm_start, pages, vma->vm_pgoff);
/* allocate and insert pages to fill the vma. */
for(i=0; i < pages; i++) {