Skip to content

Commit

Permalink
BitMap : add a simple example for API test
Browse files Browse the repository at this point in the history
Change-Id: I2dac5e59375f6512bf213e88695f202786781cf1
  • Loading branch information
54shady committed Oct 27, 2020
1 parent af115bb commit 9e2a8b4
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.cmd
*.o
*.mod.c
*.ko
*.order
Module.symvers
.tmp_versions
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
[tiny4412](https://github.com/54shady/kernel_drivers_examples/tree/tiny4412)

[mini2440](https://github.com/54shady/kernel_drivers_examples/tree/mini2440)

[x86](https://github.com/54shady/kernel_drivers_examples/tree/x86)
37 changes: 37 additions & 0 deletions debug/bitmap/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Makefile
# Comment/uncomment the following line to disable/enable debugging
# DEBUG = y

# Usage
# make CROSS_COMPILE=<cross_compiler_prefix> KERNEL_DIR=<your_kernel_dir> KERNEL_BUID_OUTPUT=<kernel_buid_output>
#
# make CROSS_COMPILE=/home/zeroway/rk3399/tool/gcc-linaro-4.9.4-2017.01-i686_aarch64-linux-gnu/bin/aarch64-linux-gnu- KERNEL_DIR=/home/zeroway/rk3399/src/firefly/kernel KERNEL_BUID_OUTPUT=/home/zeroway/rk3399/src/firefly/out/target/product/rk3399_firefly_box/obj/KERNEL

# Add your debugging flag (or not) to CFLAGS
ifeq ($(DEBUG),y)
DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlines
else
DEBFLAGS = -O2
endif

obj-m := bitmap_test.o

KERNEL_DIR ?= /lib/modules/`uname -r`/build
KERNEL_BUID_OUTPUT ?=$(KERNEL_DIR)
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
PWD := $(shell pwd)
ARCH := x86_64

modules:
$(MAKE) -C $(KERNEL_DIR) ARCH=$(ARCH) M=$(PWD) O=$(KERNEL_DIR) modules

clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions modules.order Module.symvers

depend .depend dep:
$(CC) $(CFLAGS) -M *.c > .depend

ifeq (.depend,$(wildcard .depend))
include .depend
endif
97 changes: 97 additions & 0 deletions debug/bitmap/bitmap_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <linux/init.h>
#include <linux/module.h>

/* https://www.cnblogs.com/schips/p/10674687.html */

#define BITMAP_LEN 10

#define USING_UNLONG
#ifdef USING_UNLONG
unsigned long mybm[1];
#else
DECLARE_BITMAP(mybm, BITMAP_LEN);
#endif

static int bitmap_test_init(void)
{
int bit;

/* architecture specifiy API */
/*
* non atomic version
* include/asm-generic/bitops/instrumented-non-atomic.h
*/
__set_bit(1, mybm);
/*
* arch___set_bit(nr, addr);
* asm volatile(__ASM_SIZE(bts) " %1,%0" : : ADDR, "Ir" (nr) : "memory");
*/
if (!test_bit(1, mybm))
printk(KERN_ALERT" bit 1 tested not set\n");
else
printk(KERN_ALERT" bit 1 tested set\n");

__clear_bit(1, mybm);
if (!test_bit(1, mybm))
printk(KERN_ALERT" bit 1 tested not set\n");
else
printk(KERN_ALERT" bit 1 tested set\n");

/*
* atomic version
* include/asm-generic/bitops/atomic.h
* arch/x86/boot/bitops.h
*/
set_bit(2, mybm);
if (!test_bit(2, mybm))
printk(KERN_ALERT" bit 2 tested not set\n");
else
printk(KERN_ALERT" bit 2 tested set\n");

clear_bit(2, mybm);
if (!test_bit(2, mybm))
printk(KERN_ALERT" bit 2 tested not set\n");
else
printk(KERN_ALERT" bit 2 tested set\n");

/* Generic API */
bitmap_zero(mybm, BITMAP_LEN);
if (!test_bit(3, mybm))
printk(KERN_ALERT" bit 3 tested not set\n");
else
printk(KERN_ALERT" bit 3 tested set\n");

bitmap_fill(mybm, BITMAP_LEN);
if (!test_bit(3, mybm))
printk(KERN_ALERT" bit 3 tested not set\n");
else
printk(KERN_ALERT" bit 3 tested set\n");

/* clear all before test */
bitmap_zero(mybm, BITMAP_LEN);
set_bit(4, mybm);
set_bit(8, mybm);
set_bit(9, mybm);
/* iterator all the setted bit */
for_each_set_bit(bit, mybm, BITMAP_LEN)
printk(KERN_ALERT" %d\n", bit);
printk(KERN_ALERT"===========\n");

/* iterator all the no setted bit */
for_each_clear_bit(bit, mybm, BITMAP_LEN)
printk(KERN_ALERT" %d\n", bit);
printk(KERN_ALERT"===========\n");

printk(KERN_ALERT"BitMap test Done\n");

return 0;
}

static void bitmap_test_exit(void)
{
printk(KERN_ALERT"BitMap test Exit\n");
}

MODULE_LICENSE("Dual BSD/GPL");
module_init(bitmap_test_init);
module_exit(bitmap_test_exit);

0 comments on commit 9e2a8b4

Please sign in to comment.