Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 23 Mar 2010 12:18:24 +0300
From:      Dmitry Krivenok <krivenok.dmitry@gmail.com>
To:        freebsd-hackers@freebsd.org
Subject:   Strange behavior of kernel module (output terminated)
Message-ID:  <da48cf211003230218n56794f0bs27eef860efc0da3e@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
Hello Hackers,

I'm new to FreeBSD kernel development and have a very basic question about
kernel modules.
I compiled and slightly modified an example from
http://www.freesoftwaremagazine.com/articles/writing_a_kernel_module_for_freebsd
.
Below is the source code of my first module called "hello":

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/pcpu.h>

/* The function called at load/unload. */
static int event_handler(struct module *module, int event, void *arg)
{
  int e = 0; /* Error, 0 for normal return status */
  switch (event)
    {
      case MOD_LOAD:
        uprintf("Hello FreeBSD kernel!\n");
        int i = 0;
        for(i = 0; i < 1000; i++)
          {
            uprintf("%3d ", i);
            if(! (i % 10) ) uprintf("\n");
          }
        break;
      case MOD_UNLOAD:
        uprintf("Bye Bye FreeBSD Kernel!\n");
        break;
      default:
        e = EOPNOTSUPP; /* Error, Operation Not Supported */
        break;
    }

  return(e);
};

/* The second argument of DECLARE_MODULE. */
static moduledata_t hello_conf =
{
  "hello",    /* module name */
  event_handler,  /* event handler */
  NULL            /* extra data */
};

DECLARE_MODULE(hello, hello_conf, SI_SUB_KLD, SI_ORDER_ANY);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

I compiled the module successfully and run it (via sudo make load unload).
The output confused me a bit:

$ uname -v
FreeBSD 8.0-RELEASE-p2 #0: Tue Jan  5 21:11:58 UTC 2010
root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC
$ make
Warning: Object directory not changed from original
/usr/home/krived/work/freebsd/hello
cc -O2 -pipe -fno-strict-aliasing -Werror -D_KERNEL -DKLD_MODULE -nostdinc
-I. -I@ -I@/contrib/altq -finline-limit=8000 --param inline-unit-growth=100
--param large-function-growth=1000 -fno-common  -fno-omit-frame-pointer
-mcmodel=kernel -mno-red-zone  -mfpmath=387 -mno-sse -mno-sse2 -mno-sse3
-mno-mmx -mno-3dnow  -msoft-float -fno-asynchronous-unwind-tables
-ffreestanding -fstack-protector -std=iso9899:1999 -fstack-protector -Wall
-Wredundant-decls -Wnested-externs -Wstrict-prototypes  -Wmissing-prototypes
-Wpointer-arith -Winline -Wcast-qual  -Wundef -Wno-pointer-sign
-fformat-extensions -c hello.c
ld  -d -warn-common -r -d -o hello.ko hello.o
:> export_syms
awk -f /sys/conf/kmod_syms.awk hello.ko  export_syms | xargs -J% objcopy %
hello.ko
objcopy --strip-debug hello.ko
$ sudo make load unload
/sbin/kldload -v /usr/home/krived/work/freebsd/hello/hello.ko
Hello FreeBSD kernel!
  0
  1   2   3   4   5   6   7   8   9  10
 11  12  13  14  15  16  17  18  19  20
 21  22  23  24  25  26  27  28  29  30
 31  32  33  34  35  36  37  38  39  40
 41  42  43  44  45  46  47  48  49  50
 51  52  53  54  55  56  57  58  59  60
 61  62  63  64  65  66  67  68  69  70
 71  72  73  74  75  76  77  78  79  80
 81  82  83  84  85  86  87  88  89  90
 91  92  93  94  95  96  97  98  99 100
101 102 103 104 105 106 107 108 109 110
111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130
131 132 133 134 135 136 137 138 139 140
141 142 143 144 145 146 147 148 149 150
151 152 153 154 155 156 157 158 159 160
161 162 163 164 165 166 167 168 169 170
171 172 173 174 175 176 177 178 179 180
181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200
201 202 203 204 205 206 207 208 209 210
211 212 213 214 215 216 217 218 219 220
221 222 223 224 225 226 227 228 229 230
231 232 233 234 235 236 237 238 239 240
241 242 243 244 245 246 247 248 249 250
251 252 253 254 255 256 257 258 259 260
261 262 263 264 265 266 267 268 269 270
271 272 273 274 275 276 277 278 279 280
281 282 283 284 285 286 287 288 289 290
291 292 293 294 295 296 297 298 299 300
301 302 303 304 305 306 307 308 309 310
311 312 313 314 315 316 317 318 319 320
321 322 323 324 325 326 327 328 329 330
331 332 333 334 335 336 337 338 339 340
341 342 343 344 345 346 347 348 349 350
351 352 353 354 355 356 357 358 359 360
361 362 363 364 365 366 367 368 369 370
371 372 373 374 375 376 377 378 379 380
381 382 383 384 385 386 387 388 389 390
391 392 393 394 395 396 397 398 399 400
401 402 403 404 405 406 407 408 409 410
411 412 413 414 415 416 417 418 419 420
421 422 423 424 425 426 427 428 429 430
431 432 433 434 435 436 437 438 439 440
441 442 443 444 445 446 447 448 449 450
451 452 453 454 455 456 457 458 459 460
461 462 463 464 465 466Loaded /usr/home/krived/work/freebsd/hello/hello.ko,
id=2
/sbin/kldunload -v hello.ko
Unloading hello.ko, id=2
Bye Bye FreeBSD Kernel!
$

As you can see the loop was terminated after i==466.
I tried to load/unload the module many times but the last printed number was
always 466.

Then I compiled the same module on FreeBSD-7.2 (note, the first test was run
on 8.0).
I saw exactly the same behavior except that the last number was always 550.

I don't think I found a bug in the kernel :)
I believe I just don't understand something.
Could you please explain the behavior of the module?

Thank you beforehand!

-- 
Sincerely yours, Dmitry V. Krivenok
e-mail: krivenok.dmitry@gmail.com
skype: krivenok_dmitry
jabber: krivenok_dmitry@jabber.ru
icq: 242-526-443



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?da48cf211003230218n56794f0bs27eef860efc0da3e>