[comp.sys.amiga.tech] Privilege Violations from CreateTask?

GregM@cup.portal.com (Greg Scott Miller) (07/05/89)

 I'm having an awful time lately with an annoying little bug in a
commercial project of mine.  At rare times, at the program's boot, I
receive a guru #00000008, which is a privilege violation.  However, my
code isn't invoking any privileged instructions, and again - the exception
only occurs at rare times.

 I run only two tasks - the initial process, and a second task to play
background music.  Because of where the problem occurs, and when it started
to occur, I've tracked it down to having something to do with the added
task.  I _believe_ that it is connected with the following code.

 It's an assembly version of 'CreateTask', derived partially from the
C-code listed in the back of Mortimer's "Amiga Programmer's Handbook" (V2).
I've been over the code a number of times, and cannot find a darned thing
wrong with it.

 The problem isn't with DeleteTask - the code doesn't even get that far.
Also, the since the problem doesn't always (usually doesn't) occur, I've
wondered if I've forgotten to initialize a parameter, and it _usually_
is already set to an acceptable value, out of luck.

 I'm not running a 68010, so that isn't the problem.

 Anyways, I'd appreciate any insight anyone might have.  I've got to track
this thing down, or I'll have quite a few screaming users complaining of
'buggy software'.

 Thanks!

 ---

 Here's the code - it's not sleak.  Since I didn't need to grab every
possible cycle, I didn't knock myself out for elegance:


*
* CreateTask
*
*     Entry:
*            a0 - points to the task name
*            a1 - points to the task entry point
*            d0 - gives the task's stack size
*            d1 - this task's required priority
*            a6 - points to ExecBase
*
*     Exit:
*            a0 - points to the TCB of the new task
*            d0 - return status:
*                 00 - if no error
*                 01 - cannot allocate memory
*
CreateTask:
  and.l     #$FFFFFFFC,d0               ** Make sure d0 is longword 'aligned'
  move.l    d0,d4                       ** Save it in D4
  add.l     #1+TC_SIZE,d0               ** + TCB Struct size + 1

  movem.l   d0/d1/a0/a1,-(sp)           ** So AllocMem doesn't trash 'em

  move.l    #MEMF_PUBLIC+MEMF_CLEAR,d1  ** Get the needed memory
  jsr       _LVOAllocMem(a6)
  tst.l     d0                          ** If not available, return w/
  bne       1$                          ** error status.
  moveq     #1,d0
  add.l     #16,sp
  rts
1$

  move.l    d0,a2                       ** point to start of memory
  add.l     #TC_SIZE,d0                 ** Find btm of stack space
  move.l    d0,TC_SPLOWER(a2)
  add.l     d4,d0                       ** btm + size = top
  add.l     #1,d0
  and.l     #$FFFFFFFE,d0               ** Make sure it's word aligned
  move.l    d0,TC_SPUPPER(a2)
  move.l    d0,TC_SPREG(a2)

  move.b    #NT_TASK,LN_TYPE(a2)        ** Set the type of node

  movem.l   (sp)+,d0/d1/a0/a1
  move.b    d1,LN_PRI(a2)               ** Set the task's priority
  move.l    a0,LN_NAME(a2)              ** It's name ...
  move.l    a2,-(sp)                    ** Save ptr to TCB

  exg.l     a1,a2                       ** exec wants 'em reversed
  move.l    #0,a3                       ** I need no finalization code
  jsr       _LVOAddTask(a6)             ** Add it to the system list
  move.l    (sp)+,a0                    ** and, exit pointing to the TCB

  moveq     #0,d0                       ** No errs
  rts



*
*
* DeleteTask - Deletes a task created with CreateTask
*
*     Entry - a0 points to the task's TCB
*
*     Exit - None
*
DeleteTask:

         move.l      a0,-(sp)                ** Remove it from the system
         move.l      a0,a1                   ** list
         jsr         _LVORemTask(a6)
         move.l      (sp)+,a1

         move.l      TC_SPUPPER(a1),d0       ** Free up it's memory
         sub.l       a1,d0
         add.l       #1,d0
         jsr         _LVOFreeMem(a6)
         rts


+--------------------------------------------------------------------------+
| No similarities are intended in the above | Greg Miller                  |
| article to real people; living, dead, or  | ionic!gm@frith.egr.msu.edu   |
| walking the earth in ghostly torment.     |                              |
|  - Douglas Adams                          | ..!mailrus!frith!ionic!gm    |
+--------------------------------------------------------------------------+

 (In the event that you cannot mail to the above, feel free to mail to me at
  gregm@cup.portal.com)